Fibonacci Series Program in C++. Print the Fibonacci series up to a given number.

Fibonacci Series :  It is one of the famous series in maths and also a favorite program for the interviewer. In 8 out of 10 interviews the Fibonacci series program is asked so you should definitely prepare it. Below we have solved the code for series up to a given term / number.



Program to print Fibonacci Series

Logic :

  • Keep the first term as 0 & second term as 1.
  • Input the number till you want to print the series.'
  • Print first term
  • Calculate the sum of two terms store in first and print
  • Keep on repeating till sum sum does not exceed our given number.

Code : 


#include<bits/stdc++.h>
using namespace std;


int main()
{

    int a,b,temp,t;
    a=0; b=1;
    cin >> t;


    while(a<t)
    {
        cout<<a<<" ";
        temp=a+b;
        a=b;
        b=temp;
    }

    return 0;
}


***

!! SUBSCRIBE FOR MORE CODES & VIDEOS !!

Post a Comment

0 Comments