Wednesday 9 October 2013

Filled Under: ,

Write a Program in C to Print Fibonacci Series

OBJECTIVE : Write a Program in C to Print Fibonacci Series

The Fibonacci Sequence is the series of numbers formed by adding preceding 2 numbers:

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

The next number is found by adding up the two numbers just before it.
  • The 2 is found by adding the two numbers before it i.e. (1+1)
  • Similarly, the 3 is found by adding the two numbers before it i.e. (1+2),
  • And the 5 is (2+3),
  • and so on!




Example: the next number in the sequence above would be 21+34 = 55

So here's a program in C language to print or generate Fibonacci series:

#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
int a,b,c,flag=2,i,n; 
clrscr(); 
a=0; 
b=1; 
printf("\n How many times generate series ? ");
scanf("%d",&n);
printf("\n----> FIBONACCI SERIES<----\n");
printf("%d\n%d",a,b); 
for(i=0;i<n;i++) 
{ 
c=a+b; 
a=b; 
b=c; 
printf("\n%d",c);flag++; 
if(flag==n) 
break;
} 
getch(); 
} 


The output of the program will be like this :

0 comments:

Post a Comment