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, nextTerm
  Step 2->Initialize the variables, a=0, b=1, and nextTerm = 0
  Step 3->Enter the number of terms of Fibonacci series to be printed
  Step 4->Repeat below steps n times
   -> print the value of a
   -> nextTerm = a + b
   -> a = b
   -> b = nextTerm
   -> increase value of i each time by 1
STOP


Fibonacci Series Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include<stdio.h> 
    int fib(int n) 
    { 
    	int a, b, nextTerm, i;
    	a = 0;
    	b = 1;
    	for (i = 1; i <= n; ++i) {
            printf("%d ", a);
            nextTerm = a + b;
            a = b;
            b = nextTerm;
        }
    } 
    int main () 
    { 
    int n = 9; 
    fib(n);
    return 0; 
    } 
    
    #include<bits/stdc++.h> 
    using namespace std; 
    int fib(int n) 
    { 
    	int a, b, nextTerm, i;
    	a = 0;
    	b = 1;
    	for (i = 1; i <= n; ++i) {
            printf("%d ", a);
            nextTerm = a + b;
            a = b;
            b = nextTerm;
        } 
    } 
    int main () 
    { 
    int n = 9; 
    fib(n);
    return 0; 
    } 
    
    public class Main
    {
    	public static void main(String[] args) {
    		int a, b, nextTerm, i, n =9;
        	a = 0;
        	b = 1;
        	for (i = 1; i <= n; ++i) {
                System.out.print(a);
                System.out.print(" "); 
                nextTerm = a + b;
                a = b;
                b = nextTerm;
            }  
    	}
    } 
    
    n = 9
    a = 0
    b = 1
    nextTerm = 0
    for i in range(n):
    	print(nextTerm, end = " ")
    	a = b
    	b = nextTerm
    	nextTerm = a + b
    
    using System;  
    public class Demo{  
       public static void Main(string[] args){  
          int a = 0, b = 1, nextTerm, i, n = 9;
          for(i=0 ;i < n; ++i){    
             Console.Write(a+" ");
             nextTerm = a + b;   
             a = b;    
             b = nextTerm;    
          }    
       }  
    }
    
    function Fibonacci($n){
      
        $a = 0;
        $b = 1;
      
        $counter = 0;
        while ($counter < $n){
            echo ' '.$a;
            $nextTerm = $b + $a;
            $a = $b;
            $b = $nextTerm;
            $counter = $counter + 1;
        }
    }
      
    $n = 9;
    Fibonacci($n);
    
    #include<stdio.h> 
    int fib(int n) 
    { 
    	int a, b, nextTerm, i;
    	a = 0;
    	b = 1;
    	for (i = 1; i <= n; ++i) {
            printf("%d ", a);
            nextTerm = a + b;
            a = b;
            b = nextTerm;
        }
    } 
    int main () 
    { 
    int n = 9; 
    fib(n);
    return 0; 
    } 
    

    Output

    0 1 1 2 3 5 8 13 21
    




    Recommended Programs

       Program to find factorial of a number
       Leap Year Program