/* C program to generate the first n terms of the Fibonacci sequence */
#include<stdio.h>
#include<conio.h>
main()
{
int n, count;
int a = 0,b = 1,temp = 0;
clrscr();
printf("Enter the number of terms: ");
scanf("%d",&n);
if(n == 1)
{
printf("%d",0);
}
else if(n == 2)
{
printf("%d %d ",0,1);
}
else
{
printf("%d %d ",a,b);
/*Logic to generate terms in a fibonacci series*/
for(count = 2; count < n; count++)
{
temp = a+b;
a = b;
b = temp;
printf("%d ",b);
}
}
getch();
}
Sample input and output:
Enter the number of terms: 10
0 1 1 2 3 5 8 13 21 34
No comments:
Post a Comment