Ex: Write a C program to print Fibonacci series. How to write a C program to print Fibonacci series. C program for Fibonacci series.
Input from user:
Enter number for how many times generate series: 7
Expected output:
Fibonacci series is: 0 1 1 2 3 5 8 13 21
1. First declare intiger variable's no(number), a=0, b=1, c, i .
2. After that accept input (number) from user.
3. print value of a and b directly. (and decrease number by 2 in the for loop)
4. After that use one for loop which looks like.
for(i=0;i<no-2;i++)
5. Inside the for loop Fibonacci series calculate using..
c=a+b;
a=b;
b=c;
6. After that Print value of c inside the for loop.
#include<stdio.h>
int main()
{
int no,a=0,b=1,c,i;
printf("Enter number for how many times generate series:");
scanf("%d",&no);
printf("FIBONACCI SERIES: ");
printf("%d %d ",a,b);
for(i=0;i<no-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
return 0;
}
Above prgram shows the following output:
Input from user:
Enter number for how many times generate series: 7
Expected output:
Fibonacci series is: 0 1 1 2 3 5 8 13 21
Step by step logic of the given program:
1. First declare intiger variable's no(number), a=0, b=1, c, i .
2. After that accept input (number) from user.
3. print value of a and b directly. (and decrease number by 2 in the for loop)
4. After that use one for loop which looks like.
for(i=0;i<no-2;i++)
5. Inside the for loop Fibonacci series calculate using..
c=a+b;
a=b;
b=c;
6. After that Print value of c inside the for loop.
C program to print Fibonacci series :
#include<stdio.h>
int main()
{
int no,a=0,b=1,c,i;
printf("Enter number for how many times generate series:");
scanf("%d",&no);
printf("FIBONACCI SERIES: ");
printf("%d %d ",a,b);
for(i=0;i<no-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
return 0;
}
Above prgram shows the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇