Saturday 23 April 2011

Write a C program to Fibonacci Sequence..

 A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.



#include <stdio.h>
void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();
printf("PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES");
printf("\nENTER LENGTH OF SERIES (N) : ");
scanf("%d",&no);
printf("\nFIBONACCI SERIES");
printf("\t%d %d",num1,num2);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf(" %d",fab);
num1=num2;
num2=fab;
}
getch();
}


Output:
PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES
ENTER LENGTH OF SERIES (N) : 5
FIBONACCI SERIES 0 1 1 2 3

No comments:

Post a Comment