Challenge 24

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.
      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
      5. 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
        }
    • 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

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0024

Print Requirements