BHXSpecter

Fibonnaci

Nov 11th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. // fibonacci - just to see if i know what the hell I'm doing
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int current, previous1, previous2, nthFibonacci;
  8.     bool done = false;
  9.    
  10.     cout << "Enter the first two numbers of the fibonacci: ";
  11.     cin >> previous1 >> previous2;
  12.     cout << "Enter the nth Fibonacci you want: ";
  13.     cin >> nthFibonacci;
  14.     cout << endl;
  15.    
  16.     if (!cin.good())
  17.     {
  18.         cin.clear();
  19.         cin.ignore(100, '\n');
  20.         cout << "Numbers only you letter-aholic!" << endl;
  21.     }
  22.     else if(previous1 <= 0 || previous2 <= 0 || nthFibonacci <= 0)
  23.     {
  24.         cout << "Enter numbers over 0 for all prompts." << endl;
  25.     }
  26.     else
  27.     {
  28.         cout << previous1 << " " << previous2 << " ";
  29.        
  30.         while (!done)
  31.         {
  32.             if (nthFibonacci == 1)
  33.             {
  34.                 current = previous1;
  35.             }
  36.             else if (nthFibonacci == 2)
  37.             {
  38.                 current = previous2;
  39.             }
  40.             else
  41.             {
  42.                 int counter = 3;
  43.                 while (counter <= nthFibonacci)
  44.                 {
  45.                     current = previous1 + previous2;
  46.                     previous1 = previous2;
  47.                     previous2 = current;
  48.                     cout << current << " ";
  49.                     counter++;
  50.                 }
  51.             }
  52.             cout << "\nThe " << nthFibonacci << "th fibonacci is : " << current << endl;
  53.             done = true;
  54.         }
  55.     }
  56.     return 0;
  57. }
  58.            
  59.  
Advertisement
Add Comment
Please, Sign In to add comment