Fibonacci Series : Display Fibonacci Series Using C.

Fibonacci Series : Display Fibonacci Series Using C.


In mathematics, a series such that each number is the sum of the two previous ones, starting from 0 and 1, known as Fibonacci series. The terms in this series can be signified as :- a, b, a+b, a+2b, 2a+3b, 3a+5b, 5a+8b, ....... so on. 


What is the nth Fibonacci number?

Nth term of the series is sum of the two previous ones, which is [ Fn = Fn-1 + Fn-2 ], where "Fn" is the Nth term of series and "Fn-1" and "Fn-2" are first previous term and second previous term of series.


How do you display the Fibonacci series?

  1. Take value of N from user. Where N is number of terms in the series.
  2. Use loop to display every terms and swap value of variable "a" and "b".
  3. Initial value of variable "a" and "b" are 0 and 1.
  4. Loop range will be from 1 to N.
  5. Display the terms of the series as result.

Program to find sum of the Fibonacci series.

  • C program to display series.
  • C++ program to display series.
  • Java program to display series.

Recursive program to display the Fibonacci series.

  • C program to display series using recursion.
  • C++ program to display series using recursion.
  • Java program to display series using recursion.


C program to display series.

#include <stdio.h>
int main()
{
    int na = 0b = 1i;
    printf("Type any number >>| ");
    scanf("%d", &n);
    printf("Here is your series....\n");
    for (i = 1i <= ni++)
    {
        a = a + b;
        b = a - b;
        printf("%d"a);
        if (i == n)
        {
            break;
        }
        printf(" + ");
    }
    getchar();
    getchar();
}

Output :-

Type any number >>| 10
Here is your series.... 
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55


C++ program to display series.

#include <iostream>
using namespace std;

int main()
{
    int na = 0b = 1;
    cout<<"Type any number >>| ";
    cin>>n;
    cout<<"Here is your series...."<<endl;
    for (int i = 1i <= ni++)
    {
        a = a + b;
        b = a - b;
        cout<<a;
        if (i == n)
        {
            break;
        }
        cout<<" + ";
    }
    getchar();
    getchar();
}

Output :-

Type any number >>| 10
Here is your series....
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55


Java program to display series.

public class FibonacciSeries {
    public static void main(String[] args) {
        int n = 7a = 0b = 1;
        System.out.println("Here value of N >>| 7");
        System.out.println("Here is your series....");
        for (int i = 1i <= ni++) {
            a = a + b;
            b = a - b;
            System.out.print(a);
            if (i == n) {
                break;
            }
            System.out.print(" + ");
        }
    }
}

Output :-

Type any number >>| 7
Here is your series....
1 + 1 + 2 + 3 + 5 + 8 + 13


C program to display series using recursion.

#include <stdio.h>

void fibonacci(int aint bint n);

int main()
{
    int na = 0b = 1;
    printf("Type any number >>| ");
    scanf("%d", &n);
    printf("Here is your series....\n");
    fibonacci(abn);
    getchar();
    getchar();
}

void fibonacci(int aint bint n)
{
    if (n > 0)
    {
        a = a + b;
        b = a - b;
        printf("%d"a);
        if (n == 1)
        {
            return;
        }
        printf(" + ");
        fibonacci(abn - 1);
    }
}

Output :-

Type any number >>| 10
Here is your series....
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55


C++ program to display series using recursion.

#include <iostream>
using namespace std;

void fibonacci(int aint bint n);

int main()
{
    int na = 0b = 1;
    cout<<"Type any number >>| ";
    cin>>n;
    cout<<"Here is your series...."<<endl;
    fibonacci(abn);
    getchar();
    getchar();
}

void fibonacci(int aint bint n)
{
    if (n > 0)
    {
        a = a + b;
        b = a - b;
        cout<<a;
        if (n == 1)
        {
            return;
        }
        cout<<" + ";
        fibonacci(abn - 1);
    }
}

Output :-

Type any number >>| 10
Here is your series....
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55


Java program to display series using recursion.

public class FibonacciSeries {
    void fibonacci(int aint bint n) {
        if (n > 0) {
            a = a + b;
            b = a - b;
            System.out.print(a);
            if (n == 1) {
                return;
            }
            System.out.print(" + ");
            fibonacci(abn - 1);
        }
    }

    public static void main(String[] args) {
        Fibonacci6 f = new Fibonacci6();
        int n = 7a = 0b = 1;
        System.out.println("Here value of N >>| 7");
        System.out.println("Here is your series....");
        f.fibonacci(abn);
    }
}

Output :-

Type any number >>| 7
Here is your series....
1 + 1 + 2 + 3 + 5 + 8 + 13


Attention readers! Don't stop learning now. Check out our articles to gain more knowledge.

Reactions

Post a Comment

0 Comments