Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Saturday 6 April 2013

C Program To Generate Fibonacci Series

Hi Friends,
Today, we'll learn how to generate a Fibonacci Series using C. The program and concept are pretty simple. But before that, let me tell you what exactly is a Fibonacci series.  Fibonacci series is a special series in which the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. By going this way, the series generated will be:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and so on...

The program is very simple.

First input is taken from the user that tells how many terms of the Fibonacci series will be generated and then as described above, a function is called in a loop to add the previous two numbers and generate the next number in the series.


Here is the C Code for Fibonacci Series:


/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

int fib(int n)
{
 if(n==1||n==2)
 return 1;

 else
 return(fib(n-1)+fib(n-2));
}

void main()
{
 int i,n;
 clrscr();
 printf("Enter the number of terms: ");
 scanf("%d",&n);

 printf("\nThe Series is : \n\n0");

 for(i=1;i<n;i++)
 {
  printf("%5d",fib(i)); // fib Function Called
 }
 getch();
}


Output:



C Program To Generate Fibonacci Series



Please Comment If You Liked This Post !! 


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: