4. Program to generate,first n number of elements in the Fibonacci series?
Algorithm
Step 1:Start
Step 2:Declare variables n,i=0,j=1,temp,k.
Step 3:Input and read the limit of the Fibonacci series,n.
Step 4:If n==1 then
4.1)Print i and j.
Step 5:Else
5.1)Print i and j.
5.2)Initialize k as 2.
5.3)while(k<n) repeat steps a to e.
a)Find temp= i+j.
b)Print temp.
c)Assign i=j.
d)Assign j=temp.
e)Increment k by 1.
5.4)End while.
Step 6:End if.
Step 7:Stop.
Program
#include<stdio.h>
#include<conio.h>
void
main()
{
int n,i=0,j=1,temp,k;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("fibonacci series\n");
if(n==1)
{
printf("%d\n%d\n",i,j);
}
else
{
printf("%d\n%d\n",i,j);
for(k=2;k<n;k++)
{
temp=i+j;
printf("%d\n",temp);
i=j;
j=temp;
}
}
getch();
}
Output
Enter the limit:13
fibonacci series
0
1
1
2
3
5
8
13
21
34
55
89
144
No comments:
Post a Comment