Print Fibonacci Series up to Nth Term

Print Fibonacci Series up to Nth Term” is one of the most popular problem of programming. Here, we are given a number ‘n’, entered by user and our task is to print the Fibonacci series up to nth Term.

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,…

The first terms of Fibonacci series are fixed and are 0 and 1. All the succeeding numbers of the Fibonacci series are sum of preceding two numbers.

Like, First term = 0, Second Term = 1

Third Term = Second Term(1) + First Term(0) = 1

Fourth Term = Third Term(1) + Second Term(1) = 2

Fifth Term = Fourth Term(2) + Third Term(1) = 3

.

.

.

So on…

Formula:
F(n) = F(n-1) + F(n-2)

Example

INPUT:
N = 5
OUTPUT:
0 1 1 2 3
INPUT:
N = 7
OUTPUT:
0 1 1 2 3 5 8

C++ Program to print the Fibonacci series up to nth number is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    //Scan the number  
    int n;  
    cout<<"Enter the number:";  
    cin>>n;  
      
    // Computing and Printing the fibonacci series upto nth number  
    int first = 0;  
    int second = 1;  
    int nth;  
    cout<<"The fibonacci series :\n";  
    for(int i = 1; i <= n ; i++)  
    {  
        // Printing the first fibonacci number  
        if(i == 1)  
        {  
            cout<<first<<" ";  
        }  
        // Printing the second fibonacci number  
        else if(i == 2)  
        {  
            cout<<second<<" ";  
        }  
        // Printing the rest of fibonacci series number  
        else  
        {  
            nth = first + second;  
            first = second;  
            second = nth;  
            cout<<nth<<" ";  
        }  
    }  
}  

OUTPUT:
Enter the number: 6
The Fibonacci series:
0 1 1 2 3 5

Related Posts:

  1. Program to find and print nth Fibonacci number.
  2. Program to find Power of the Number.
  3. Program to find Quotient and Remainder.
  4. Program to find largest amongst three numbers.
  5. Program to find factorial of a number.
  6. Program to find GCD of two numbers.
  7. Program to find LCM of two numbers.
  8. Program to check whether entered number is odd or even.
  9. Program to check whether entered number is prime number or not.
  10. Program to check whether entered number is palindrome or not.
  11. Program to check whether entered number is Armstrong Number or Not.
  12. Program to convert binary number to octal number.
  13. Program to convert binary number to decimal number.
  14. Program to convert binary number to hexadecimal number.
  15. Program to convert octal number to binary number.
  16. Program to convert octal number to decimal number.
  17. Program to convert octal number to hexadecimal number.
  18. Program to convert decimal number to binary number.
  19. Program to convert decimal number to octal number.
  20. Program to convert decimal number to hexadecimal number.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *