Algorithm


Fibonacci numbers, are called as Fn, which make a sequence, called the Fibonacci sequence, It creates that sequence such that every number is the sum of the two preceding ones, which starts from 0 and 1.

 

First Let's look a Basic fibonacci series to get a clearer example of Fibonacci Sequence - 

 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

 

Fibonacci Formula is: 

Fn = Fn-1 + Fn-2

 

Let's See the sequence:

  • f(0) = 0 (assume)
  • f(1) = 1
  • f(2) = f(1) + f(0) = 1
  • f(3) = f(2) + f(1) = 1 + 1 = 2
  • f(4) = f(3) + f(2) = 2 + 1 = 3
  • f(5) = f(4) + f(3) = 3 + 2 = 5
  • f(6) = f(5) + f(4) = 5 + 3 = 8
  • f(7) = f(6) + f(5) = 8 + 5 = 13
  • f(8) = f(7) + f(6) = 13 + 8 = 21
  • and ongoging like this Fn = Fn-1 + Fn-2

 

 

Code Examples

#1 Fibonacci Number Basic Code Example with Input

Code - C Programming

#include <stdio.h>
int main() {
    int i, howMany, t1 = 0, t2 = 1, nextNo;
    printf("Enter how many times print : ");
    scanf("%d", &howMany);
    
    printf("Fibonacci Series Are : ");
    for (i = 1; i  < = howMany; ++i) {
        printf("%d, ", t1);
        nextNo = t1 + t2;
        t1 = t2;
        t2 = nextNo;
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10

Output

x
+
cmd
Enter how many times print : Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

#2 Fibonacci Numbers with C++ Programming

Code - C++ Programming


#include <iostream>
using namespace std;
int main(void){
    int n = 10; 
    int a = 0, b = 1, c, i; 
    
    cout << "Fibonacci Numbers: ";
    if( n == 0){
        return a;
    }
         
    for(i = 2; i <= n; i++> 
    { 
        c = a + b; 
        a = b; 
        b = c; 
        cout  << c << ',';
    } 
    return 0; 
}

Copy The Code & Try With Live Editor

Output

x
+
cmd
Fibonacci Numbers: 1,2,3,5,8,13,21,34,55,

#3 Fibonacci Program in Java

Code - Java Programming


import java.util.*;

public class Main {
   
    static int fibonacci(int n) 
    { 
        int a = 0, b = 1, c; 
        if (n == 0){
            return a;
        }
        
        for (int i = 2; i  < = n; i++) 
        { 
            c = a + b; 
            a = b; 
            b = c; 
            System.out.print(c+",");
        }
        return b;
    } 
     
    public static void main(String[] args) throws Exception {
        int n = 10; 
        System.out.println ("Total Upto: "+fibonacci(n)); 
    }
    
}

Copy The Code & Try With Live Editor

Output

x
+
cmd
1,2,3,5,8,13,21,34,55,Total Upto: 55
Advertisements

Demonstration


Let's Start Basic Demonstration of this Fibonacci Program - 

int i, howMany, t1 = 0, t2 = 1, nextNo;

for (i = 1; i <= howMany; ++i) {
    printf("%d, ", t1);
    nextNo = t1 + t2;
    t1 = t2;
    t2 = nextNo;
}

 

t1=0, t2=1, i = 1,

  • t1 = 0, nextNo = 0+1 = 1, t1 = t2 = 1, t2 = nextNo =1
  • t1 = 1, nextNo = 1+1 = 2, t1 = t2 = 1, t2 = nextNo = 2
  • t1 = 1, nextNo = 1+2 = 3, t1 = t2 = 2, t2 = nextNo = 3
  • t1 = 2, nextNo = 1+3 = 4, t1 = t2 = 3, t2 = nextNo = 4
  • t1 = 3, and going on...

 

Applications of Fibonacci Sequence:

  1. It is used for computational run time analysis like Euclids Algorithm to determine the greatest common divisor of two integers.
  2. Fibonacci Numbers are an example of complete Sequence
  3. Every Positive number can be written in a unique way like this Fibonacci Sequence
Next
Appending into a File in C Programming