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.
Program Code:
#include<stdio.h>
void main()
{
int a,b,n,c,i;
printf("\n Enter a number of terms:");
scanf("%d",&n);
a=0;
b=1;
for(i=0;i<=n-2;i++)
{
if(a==0 && b==1)
printf("the fibonacci sequence is %d\t%d",a,b);
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}
Post A Comment:
0 comments: