The arrangement of numbers in the below-mentioned pattern is called Fibonacci series:
1 1 2 3 5 8 13 21 34 55 ….
We start with the numbers 0 and 1 and print them. Then, the next number in the series would be the sum of previous two numbers.
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
Fibonacci series using For loop in C
#include<stdio.h>
main()
{
int a=0,b=1,c = 1;
int i;
int lengthOfFibonacci;
printf("Enter how long do you want to display the Fibonacci series? ");
scanf("%d", &lengthOfFibonacci);
for(i=1;i<=lengthOfFibonacci;i++)
{
printf("%d ",c);
c = a + b;
a = b;
b = c;
}
}Fibonacci series using While in C
#include<stdio.h>
main()
{
int a=0,b=1,c = 1;
int i=1;
int lengthOfFibonacci;
printf("Enter how long do you want to display the Fibonacci series? ");
scanf("%d", &lengthOfFibonacci);
while(i<=lengthOfFibonacci)
{
printf("%d ",c);
c = a + b;
a = b;
b = c;
i++;
}
}Program OUTPUT
Enter how long do you want to display the Fibonacci series? 10
1 1 2 3 5 8 13 21 34 55
Leave a Reply