|
-
October 24th, 2010, 01:19 PM
#1
Fibonacci Sequence
Can someone throw me a hint on how to only print the end result of an iteration? This is in regards to Case C in the program. I just copied and pasted the loop I have in Case B, but like I said I don't want to print the whole sequence, I only want to print the final result.
Code:
#include <iostream>
using namespace std;
void PrintHistory();
int main ()
{
char choice;
int n = 1,
a = 1,
b = 0,
sum;
do
{
// Prompts users to enter a menu selection.
cout << "A. Facts about the Fibonacci Sequence." << endl;
cout << "B. Output the first N Fibonacci Numbers." << endl;
cout << "C. Output the Nth Fibonacci Number." << endl;
cout << "D. Mystery Number Sequence." << endl;
cout << "E. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
// Respond to the user's menu selection.
switch (choice)
{
case 'A':
case 'a':
PrintHistory();
cout << endl;
break;
case 'B':
case 'b':
cout << "How many numbers do you want to compute: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
cout << endl << sum << endl;
}
cout << endl;
break;
case 'C':
case 'c':
cout << "How many numbers do you want to compute: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
cout << sum << endl;
}
cout << endl;
break;
case 'D':
case 'd':
cout << "You entered D." << endl;
cout << endl;
break;
case 'E':
case 'e':
cout << "Quit." << endl;
cout << endl;
break;
default:
cout << "Please enter A, B, C, D, or E." << endl;
cout << endl;
break;
}
}
while (choice != 'E' && choice != 'e');
return 0;
}
void PrintHistory()
{
cout << "The Fibonacci Sequence is based on a recursive formula." << endl;
cout << "The subsequent number is the sum of the two values that precedes it." << endl;
cout << "The sequence is prominent in nature such as in the seashell, snails, sunflowers." << endl;
cout << endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|