Fibonacci Series in C

dheerajpatidar.com

In this post I’m gonna share you C program to print Fibonacci Series in easy to understand way.

Do you know What is Fibonacci Series? If no, then lets first understand the Fibonacci Series.

What is Fibonacci Series?

The Fibonacci series is a sequence of numbers where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 and 1.

So, as per the definition we will get the series: 0, 1, 1, 2, 3, 5, 8, 13, 21,…..n.

Fibonacci Series in C

There are two ways to write the Fibonacci series program:

  • Without recursion
  • With recursion

Fibonacci Series in C without Recursion

 

 #include <stdio.h>

 

 int main(){

    int n, i, a,b,c;

 

    printf(“Enter Number of Terms: “);

    scanf(%d,&n);

 

    a=0;

    b=1;

    c=0;

 

    printf(“Fabonacci Series upto given term: “);

    for(i=0; i<n; i++){

        printf(%d,c);

        a=b;

        b=c;

        c=a+b;

    }

    return 0;

 }

 

Output:

  Enter Number of Terms: 8

Fibonacci Series upto given term: 0 1 1 2 3 5 8 13

How the above Program will work?

1) The above program is about Printing Fibonacci Series without recursion.

2) Here we have taken some integer variables a, b, and c to manipulate the values. n variable is for storing user input, i is for the the for loop’s iterations we will use in the program.

 int n, i, a,b,c;

3) After that. we have asked the user that “Enter Number of Terms: “, to print number of terms of Fibonacci series, and store it into variable n that we have declared before.

  printf(“Enter Number of Terms: “);

scanf(%d,&n);

4) Now, after taking user input, I’ve initialised all auxiliary integer variables a,b, and c with 0, 1, and 0, because a, & b are considered as initial two terms, and in c we will add sum of a & b using loop.

They can be initialised during the declaration.

  a=0;

 b=1;

 c=0;

5) Now, using for loop we will print the whole Fibonacci series upto the term user has entered.

  printf(“Fabonacci Series upto given term: “);

 for(i=0; i<n; i++){

     printf(%d,c);

     a=b;

     b=c;

     c=a+b;

 }

Fibonacci Series in C with Recursion

#include<stdio.h>

void Fibonacci(int n){

static int a=0,b=1,c;

if(n>0){

c = a + b;

a = b;

b = c;

printf(%d,c);

Fibonacci(n1);

}

}

int main(){

int n;

printf(“Enter Number of Terms: “);

scanf(%d,&n);

printf(“Fabonacci Series upto given term: “);

printf(%d %d,0,1);

Fibonacci(n2);//n-2 because 2 numbers are already printed

return 0;

}

Output:

  Enter Number of Terms: 10

 Fibonacci Series upto given term: 0 1 1 2 3 5 8 13 21 34

How the above Program will work?

1) The above program is about Printing Fibonacci Series using recursion.

2) Here we have declare a variable n to take user’s input.

 int n;

3) After that. we have asked the user that “Enter Number of Terms: “, to print number of terms of Fibonacci series, and store it into variable n that we have declared before.

printf(“Enter Number of Terms: “);

scanf(%d,&n);

4) Now, I’ve print the message “Fibonacci Series upto given term: ” & print the first 2 terms of the series manually.

printf(“Fabonacci Series upto given term: “);

printf(%d %d,0,1);

5) After that we have passed n-2 in the function Fibonacci() in order to print remaining terms.

  Fibonacci(n-2);//n-2 because 2 numbers are already printed 

6) Lets, understand what Fibonacci function will do:

void Fibonacci(int n){

static int a=0,b=1,c;

if(n>0){

c = a + b;

a = b;

b = c;

printf(%d,c);

Fibonacci(n1);

}

}

  • First of all the Fibonacci function takes value taht have been passed.
  • I’ve declared a, b, & c variables, where a = 0, b = 1.
  • Now, I’ve use if statement with n>0 condition, which means if the value of n is greater than 0 then, execute the code written inside.
  • Now inside the if body I’ve do some manipulation, which is: c = a + b; a = b; b = c;
  • After this we simply print the value of c which is the new term of the series.
  • After that I have passed n-1 in Fibonacci function. Now If statement will execute until the value of n become 0.

Conclusion

In this post we have learn Fibonacci Series in C, where we discuss both with & without recursion approach.

In order to clear all the Basics of C Programming, you can visit my C Programming Course for Free.

I hope you have got useful content from here. I’m trying to share my best with you.

For any query about this post, you can comment us.

Thanks for Visit!

Leave a Reply

Your email address will not be published. Required fields are marked *