Question

Description The purpose of this challenge is to various flow looping and control structures. This challenge...

Description

The purpose of this challenge is to various flow looping and control structures. This challenge displays a menu and asks the user to perform various tasks.

Requirements

  1. Show a menu to the user with the following choices: a, b, c, q (Show the menu once and use a do-while statement to prompt the user for a menu command):
    • a – Display all odd numbers between n1 and n2.
      • Ask the user to enter two numbers, n1 and n2, using the int data type.
      • Using any looping mechanism (determine which loop works best), display all odd numbers between n1 and n2
    • b – Determine if a number is in the Fibonacci sequence (Wiki, Pop Culture references: The Davinci Code, 21) . The Fibonnacci sequence starts with the numbers 1 and 1. The next number in the sequence is the sum of the preceding two numbers. In this case, 1 + 1 = 2. The 3rd term is 2. Now we have 1, 1, 2. The next term adds 1 + 2 = 3. The first few terms of the sequence looks like 1, 1, 2, 3, 5, 8, 13, 21, 34, etc.
      int term1 = 1;
      int term2 = 1;
      int next = term1 + term2;
      while (some boolean expression)
      {
        // set new values for term1, term2 and next according
        // to Fibonacci
      }
      1. Ask the user to enter a number (an integer)
      2. Take the user’s number and determine if this number can be found in the Fibonacci sequence.
      3. Show a message to the user whether it is or isn’t in the sequence.
      4. HINT: Write a loop that generates the successive terms of the Fibonacci sequence. If terms generated exceed the number you’re determining without having matched your search number, then the number is not in sequence
    • c – The sum of all integers between n1 and n2 inclusively
      • Ask the user to enter two numbers, n1 and n2, using the int data type.
      • Using any looping mechanism (determine which loop works best), calculate the sum of all numbers between n1 and n2
    • q – Quit the program
  2. Make sure your switch statement contains a default statement to cover the case when the user selects an unsupported menu option

Sample Interaction / Output

a - Display all odd
b - Is number in Fibonacci?
c - Sum of numbers between 1 and 10
q - Quit

Enter a command: f
<Unknown command>

Enter a command: a

Enter a number: 1 
Enter a number: 10 

ALL ODD: 1 3 5 7 9

Enter a command: b

Enter a number: 5
5 is in the Fibonacci sequence

Enter a command: b

Enter a number: 11
11 is not in the Fibonacci sequence

Enter a command: c

Enter a number: 1 
Enter a number: 10 

SUM: 55

Enter a command: q
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source code

import java.io.*;

import java.util.*;

class StructuresExample {

public static void main (String[] args) {

Scanner sc=new Scanner(System.in);

while(true){

System.out.println("a-display all odd");

System.out.println("b-is number in fibonacci");

System.out.println("c-sum of numbers between n1 and n2");

System.out.println("d-quit");

System.out.println("enter your command");

String ch=sc.nextLine();

System.out.print(ch);

if(ch.equals("a")){

System.out.println("enter two numbers");

int a=sc.nextInt() ;

int b=sc.nextInt() ;

int i;

if(a%2==0)

i=a+1;

else

i=a;

System.out.print("All odd: ");

for(i=i;i<=b;i=i+2){

System.out.print(i+" ");

}

}

else if(ch.equals("b")) {

System.out.println("enter a number");

int it=sc.nextInt() ;

int a=1;

int b=1;

int next=a+b;

for(;next<it;){

a=b;

b=next;

next=a+b;

if(next==it){

System.out.println(it+" is in the fibonacci series");

}

if(next>it){

System.out.println(it+" is not in the fibonacci series");

}

}

}

else if(ch.equals("c")) {

System.out.println("enter two numbers");

int a=sc.nextInt() ;

int b=sc.nextInt() ;

int sum=0;

for(int I=a;I<=b;I++){

sum=sum+I;

}

System.out.println("SUM:"+sum);

}

else if(ch.equals("d")){

System.exit(0) ;

}

else{

System.out.println("<unknown command>");

}

}

}

}

Input:

a
1
10
b
8
c
1
10
F
d

Output:


a-display all odd
b-is number in fibonacci
c-sum of numbers between n1 and n2
d-quit
enter your command a
enter two numbers 1 10
All odd: 1 3 5 7 9
a-display all odd
b-is number in fibonacci
c-sum of numbers between n1 and n2
d-quit
enter your command F
<unknown command>

a-display all odd
b-is number in fibonacci
c-sum of numbers between n1 and n2
d-quit
enter your command
benter a number 8
8 is in the fibonacci series

a-display all odd
b-is number in fibonacci
c-sum of numbers between n1 and n2
d-quit
enter your command

c enter two numbers 1 10
SUM:55

a-display all odd
b-is number in fibonacci
c-sum of numbers between n1 and n2
d-quit

enter your command
d

Add a comment
Know the answer?
Add Answer to:
Description The purpose of this challenge is to various flow looping and control structures. This challenge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • This lab is to give you more experience with CH Making Decisions and Looping Please do...

    This lab is to give you more experience with CH Making Decisions and Looping Please do the following Review Questions 1. Using the following chart, write an if else if statement that assigns. 10..15 or 20 to commission, depending on the value in sales. Make sure sales is valid (ie. >= 0) Sales Commission Rate Up to $10.000 10% $10,000 to $15,000 15% Over $15,000 20% 2. Convert the following if else if statement into a switch statement. double number...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8,...

    c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonacci number is 1, i.e. F(2) = 1 Other Fibonacci numbers in the sequence is the sum of two previous Fibonacci numbers. For example F(3) = F(2) + F(1). In general F(n) = F(n-1) + F(n-2) Write a program to do the following tasks. User entries are shown in...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • Please answere using python language codes. Write a program to print out Collatz sequence for a...

    Please answere using python language codes. Write a program to print out Collatz sequence for a user-supplied number. Prompt the user for a positive integer which will become the first number in the sequence. Next number in the sequence is derived as follows: If previous number is odd, the next number is 3 times the previous, plus 1. If previous number is even, the next number is half of the previous According to Collatz proposition, the sequence ultimately reaches 1...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT