Thursday, 22 December 2016

C++ Program to display Fibonacci Series up to n terms.

Statement : 
C++ Program to display Fibonacci Series up to n terms.
Code:
// C++ Program to display Fibonacci Series up to n terms.
#include<iostream>
using namespace std;
int main()
{
       cout << "\t\t\tFibonacci Series\n\n";
       int n, i, f, s, nx, temp; //f use for first numebr s for second number and nx for next number
       f = 0; s = 1; temp = 0;

       cout << "Enter The Number Of Terms Upto You Want Fibonacci Series : ";
       cin >> n;

       cout << "First " << n << " Terms Of Fibonacci Series Are : " << endl;

       for (i = 1; i <= n; i++)
       {
              if (i <= 1) //if numebr already 1 or less then 1
                     nx = i;
              else //if numebr greater then 1
              {
                     nx = f + s; //next number will be addition of first and sexond number              *
                     f = s; // first number will equal to second number                                 * **By Definitation Of Fibanocci Series**
                     s = nx; //second number will be equal to sum of first and second number            *
              }
              cout << nx << ", "; //print the second number and space of comma
              temp++;
              if (temp % 18 == 0) // to proper output new line will start after 18 terms will print
              {
                     cout << endl;
              } // end of format if
       } //end of for loop
       cout << "\n\n";
       system("pause");
       return 0;

}

Mohammad Tayyab

Author & Editor

Imagination is more important than knowledge.

0 comments:

Post a Comment