In this example, you are going to learn in depth about C program to display Fibonacci series till the number entered by the user.
Fibonacci sequence is the series of integers where next number in series is formed by adding previous two numbers.
0 1 (0+1)1 (1+1)2 (1+2)3 (2+3)5
As shown above, the next number is found by adding up the two numbers before it. For example:
Now, let’s implement this logic into C code to get Fibonacci series.
[adsense1]
//C program to implement fibonacci series
#include<stdio.h>
int main()
{
int oldNum, newNum, fibNum, maxNum;
//Enter maximum number till you want to generate fibonacci series
printf("\nEnter number: ");
scanf("%d", &maxNum);
oldNum = 0;
newNum = 1;
fibNum = oldNum + newNum;
printf("\nFibonacci Series: %d %d ", oldNum, newNum);
//while is true till fibNum is less than maxNum and fibNum is calculated
while(fibNum <= maxNum)
{
printf("%d ", fibNum);
oldNum = newNum;
newNum = fibNum;
fibNum = oldNum + newNum;
}
printf("\n");
return 0;
}
Output