Write A Program in C++ & Python to Display Fibonacci Series

Posted in

Write A Program in C++ & Python to Display Fibonacci Series

Vinay Khatri
Last updated on July 21, 2022

    In this program, we will code to display the Fibonacci series up to number n. A Fibonacci series is a sequence starts from 0 and the next number is the sum of the previous number and the current one. For example, 0,1, 1, 2, 3, 5, 8……. Is a Fibonacci series.

    C++ Program to Display Fibonacci Series

    #include<iostream.h>
    #include< conio.h>
    #include<stdio.h>
    #include<math.h>
    void main()
    {
    clrscr();
    int f=1, n,t,s=0;
    cout<<"How many numbers you want: ";
    cin>>n;
    for(int i=2; i<n;i++){
    t = f+s;
    cout<<t<<" " ;
    f=s;
    s= t;
    }
    getch();
    }

    Output:

    How many numbers you want: 8
    
    0 1 1 2 3 5 8

    Python Program to Display Fibonacci Series

    n = int(input("How many numbers you want: "))
    f,s =1,0
    print("0",end=" ")
    for i in range(2,n):
        t=f+s
        print(t,end=" ")
        f=s
        s=t

    Output:

    How many numbers you want: 19
    
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

    People are also reading:

    Leave a Comment on this Post

    0 Comments