Q. Write a program to print fibonacci series.

Solution :- This program is about giving a length N and the task is to print the Fibonacci series upto n terms.

Fibonacci Sequence :- The Fibonacci sequence is a sequence consisting of a series of numbers and each number is the sum of the previous two numbers. For Example :- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….

Fibonacci Series Algorithm

START
  Step 1->Declare variables i, a, b, show
  Step 2->Initialize the variables, a=0, b=1, and show = 0
  Step 3->Enter the number of terms of Fibonacci series to be printed
  Step 4->Print First two terms of series i.e a and b
  Step 5->Repeat below steps n-2 times
   -> show = a+b
   -> a=b
   -> b=show
   -> increase value of i each time by 1
   -> print the value of show
STOP


Fibonacci Series Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include<stdio.h> 
    int fib(int n) 
    { 
       if (n <= 1) 
          return n; 
       return fib(n-1) + fib(n-2); 
    } 
      
    int main () 
    { 
      int n = 9; 
      printf("%d", fib(n)); 
      getchar(); 
      return 0; 
    } 
    
    #include<bits/stdc++.h> 
    using namespace std; 
      
    int fib(int n) 
    { 
        if (n <= 1) 
            return n; 
        return fib(n-1) + fib(n-2); 
    } 
      
    int main () 
    { 
        int n = 9; 
        cout << fib(n); 
        getchar(); 
        return 0; 
    } 
      
    
    class fibonacci 
    { 
        static int fib(int n) 
        { 
        if (n <= 1) 
           return n; 
        return fib(n-1) + fib(n-2); 
        } 
           
        public static void main (String args[]) 
        { 
        int n = 9; 
        System.out.println(fib(n)); 
        } 
    } 
    
    
    def Fibonacci(n): 
        if n<0: 
            print("Incorrect input") 
        # First Fibonacci number is 0 
        elif n==0: 
            return 0
        # Second Fibonacci number is 1 
        elif n==1: 
            return 1
        else: 
            return Fibonacci(n-1)+Fibonacci(n-2) 
      
    # Driver Program 
      
    print(Fibonacci(9)) 
    
    using System;  
      
    public class LFC  
    {  
        public static int Fib(int n)  
        {  
            if (n <= 1)  
            {  
                return n;  
            }  
            else
            {  
                return Fib(n - 1) + Fib(n - 2);  
            }  
        }  
              
        // driver code 
        public static void Main(string[] args)  
        {  
            int n = 9; 
            Console.Write(Fib(n));  
        }  
    }  
    
     
    
    function fib($n) 
    { 
        if ($n <= 1) 
            return $n; 
        return fib($n - 1) +  
               fib($n - 2); 
    } 
      
    // Driver Code 
    $n = 9; 
    echo fib($n); 
      
    
    #include<stdio.h> 
    int fib(int n) 
    { 
       if (n <= 1) 
          return n; 
       return fib(n-1) + fib(n-2); 
    } 
      
    int main () 
    { 
      int n = 9; 
      printf("%d", fib(n)); 
      getchar(); 
      return 0; 
    } 
    

    Output

    34
    

    Recommended Programs

       Program to find factorial of a number
       Leap Year Program