3(b) 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.
Algorithm :
1. Start
2. Read n terms
3. set i=0,j=1
4. write i
5. write j
6. set m:=2
7. repeat steps 8 to 12 until m < n
8. set s:=i+j
9. write s
10. set i:=j
11. set j:=s
12. m=m+1
13. stop
Flowchart :
Program
#include<stdio.h>
void main()
{
int i=0,j=1,k=0,n,p;
printf("enter the number of terms.");
scanf("%d",&n);
printf("%d\t%d",i,j);
for(p=0;p<n-2;p++)
{
k=i+j;
printf("\t%d",k);
i=j;
j=k;
}
}
Algorithm :
1. Start
2. Read n terms
3. set i=0,j=1
4. write i
5. write j
6. set m:=2
7. repeat steps 8 to 12 until m < n
8. set s:=i+j
9. write s
10. set i:=j
11. set j:=s
12. m=m+1
13. stop
Flowchart :
Program
#include<stdio.h>
void main()
{
int i=0,j=1,k=0,n,p;
printf("enter the number of terms.");
scanf("%d",&n);
printf("%d\t%d",i,j);
for(p=0;p<n-2;p++)
{
k=i+j;
printf("\t%d",k);
i=j;
j=k;
}
}

Comments
Post a Comment