C++ Program to Find Fibonacci Numbers using Iteration

This C++ Program demonstrates the the computation of Fibonacci Numbers using Iteration.

Here is source code of the C++ Program to Find Fibonacci Numbers using Iteration. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Find Fibonacci Numbers using Iteration
  3.  */
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cstdlib>
  7. #define ll long long
  8. using namespace std;
  9.  
  10. /* 
  11.  * Iterative function to find Fibonacci Numbers 
  12.  */
  13. ll fibo_iter(int n)
  14. {
  15.     int previous = 1;
  16.     int current = 1;
  17.     int next = 1;
  18.     for (int i = 3; i <= n; ++i) 
  19.     {
  20.         next = current + previous;
  21.         previous = current;
  22.         current = next;
  23.     }
  24.     return next;
  25. }
  26. /* 
  27.  * Main
  28.  */
  29. int main()
  30. {
  31.     int n;
  32.     while (1)
  33.     {
  34.         cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
  35.         cin>>n;
  36.         if (n == 0)
  37.             break;
  38.         cout<<fibo_iter(n)<<endl;
  39.     }
  40.     return 0;
  41. }

$ g++ fibo_iter.cpp
$ a.out
 
Enter the integer n to find nth fibonnaci no.(0 to exit): 1
1
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
1
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
2
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
3
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
5
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
8
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
13
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
21
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
34
Enter the integer n to find nth fibonnaci no.(0 to exit): 10
55
Enter the integer n to find nth fibonnaci no.(0 to exit): 0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.