Program to Display Fibonacci Series [C, C++, Python & Java]

Posted in

Program to Display Fibonacci Series [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    If you are a beginner in the programming world, you will definitely learn to write a program to display the Fibonacci series. In this article, you will learn to write a program in C, C++, Java, and Python to display the Fibonacci series up to the number n.

    What is Fibonacci Series?

    The Fibonacci series is a sequence of terms where the current term is the sum of the two previous terms. By default, the first two terms of the Fibonacci series are 0 and 1. In some older versions, the Fibonacci series starts from 1, eliminating 0.

    For example, 0,1,1,2,3,5,8,.... is a Fibonacci series.

    Here is a formula to calculate the Fibonacci series:

    The first term, F0, is 0, and the second term, F1, is 1. The next term, F2, will be

    F2 = F0 + F1 = 0+1 = 1

    F3 = F2 + F1 = 1+1 = 2

    F4 = F3 + F2 = 2+1 = 3

    F5 = F4 + F3 = 3+2 = 5

    and so on.

    Hence, the expression to calculate the Fibonacci series becomes Fn = Fn-1 + Fn-2, where n > 1.

    C Program to Display Fibonacci Series

    #include<stdio.h>
    int main()
    {
       int n, i, t1=0, t2=1, nextTerm;
       nextTerm = t1 + t2;
       printf("Enter the number of terms in a series: ");
       scanf("%d", &n);
       printf("Fibonacci Series: %d %d", t1, t2);
       for(int i=3; i<=n; ++i)  
       {
          printf(" %d", nextTerm);
          t1 = t2;
          t2 = nextTerm;
          nextTerm = t1 + t2;   
       }
    return 0;
    }

    Output:

    Enter the number of terms in a series: 10
    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

    C++ Program to Display Fibonacci Series

    #include<iostream>
    using namespace std;
    int main()
    {
       int n, i, t1=0, t2=1, nextTerm;
       nextTerm = t1 + t2;
       cout<<"Enter the number of terms in a series: ";
       cin>>n;
       cout<<"Fibonacci Series: "<<t1<<" "<<t2;
       for(int i=3; i<=n; ++i)  
       {
          cout<<" "<<nextTerm<<" " ;
          t1 = t2;
          t2 = nextTerm;
          nextTerm = t1 + t2;   
       }
    return 0;
    }

    Output:

    Enter the number of terms in a series: 6
    Fibonacci Series: 0 1 1  2  3  5 

    Python Program to Display Fibonacci Series

    n = int(input("Enter the number of terms in a series: "))
    t1, t2 = 1,0
    print("0",end=" ")
    for i in range(2,n):
        nextTerm = t1 + t2
        print(nextTerm,end=" ")
        t1= t2
        t2 = nextTerm

    Output:

    Enter the number of terms in a series: 19
    
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

    Python Program to Display Fibonacci Sequence Using Recursion

    Recursion: Recursion is a technique in which the function repeatedly calls itself until the base condition is satisfied.

    Steps:

    • Ask the user to enter a number representing the number of integers to display from the Fibonacci series.
    • Create a recursive function that acts as a loop and calls the function again and again till we get the range entered by the user.
    def fibo(n,first =0,second =1):
        if n<1:
            print("Done with Recursion")
        else:
            f=first
            print(f)
            s=second
            now= f+s
            f= second
            s= now
           
            #recursion
            fibo(n-1,f,s)  
    
    num = int(input("Enter how many terms you want to display from Fibonacci Sequence: "))
    fibo(num)

    Output:

    Enter how many terms you want to display from Fibonacci Sequence: 15
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    Done with Recursion

    Java Program to Display Fibonacci Series

    import java.util.*;
    public class Main
    {
       public static void main(String[] args)
       {
       int n, i, t1=0, t2=1, nextTerm;
       nextTerm = t1 + t2;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of terms in a series: ");
       n = sc.nextInt(); 
       System.out.println("Fibonacci Series: " +t1+ " " +t2);
       for(i=3; i<=n; ++i)  
       {
          System.out.println(" " +nextTerm);
          t1 = t2;
          t2 = nextTerm;
          nextTerm = t1 + t2;   
       }
       }
    }

    Output:

    Enter the number of terms in a series: 
    12
    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

    Conclusion

    This was all about writing a program in C, C++, Python, and Java to display the Fibonacci series. Though implementing the logic can be pretty intimidating at the beginning, it is straightforward when you understand what exactly you need to do. We used the for loop in all the above programs. You can use a while loop or do-while loop and implement the same logic to print the Fibonacci series of n numbers.

    If you have any issues, do let us know in the comments. We would be glad to resolve your issues.

    People are also reading:

    Leave a Comment on this Post

    0 Comments