The Fibonacci series is a sequence of numbers in which the first numbers will be 0 and 1 From the next number onwards, numbers in the series will be the sum of the previous two numbers. For example, 0,1,1,2,3,5,8... where every number will be the sum of two previous numbers.
In this C program, we need to generate the Fibonacci numbers up to a given number, which means if the user enters a value of 10, we have to Calculate the Fibonacci Series below 10. In our previous Fibonacci program, we calculated 'n' Fibonacci numbers, which means if the user enters 10, we have to find the 10 Fibonacci numbers.
Here in this C program, after we have the user input in a variable called 'num,' we compare the num with the 'sum' variable. If the 'Sum' is greater than the 'num', we have to stop generating the series.
To implement Fibonacci logic, we have to Declare some integer variables 't1', 't2', 'sum'. First set 'sum=0', 't1=0', and 't2=1'. Then read the Limit of the Series into the integer variable num. Display the First two integer variables as 't1' and 't2'. Means '0' and '1', and make the next number as the Sum of 't1' and 't2'. Then open a while loop until Sum is less than num, then in the loop print sum, then interchange the values of t1 =t2 and t2 = sum. and again calculate the sum as t1 + t2.
STEP 1: Include the Header files to use the built-in functions in the C program.
STEP 2: Declare the integer variables "sum, num, t2, t1 and set sum=0, t1=0, t2=1".
STEP 3: Read the Limit to generate the Fibonacci Series from the user and store it into num.
STEP 4: Display the first two numbers of the Fibonacci Series as 0 and 1.
STEP 5: Find sum=t1+t2. Which is the next element in the Fibonacci Sequence.
STEP 6: By using a 'while loop
' check the Sum is less than num.
STEP 7: Then Assign 't1=t2' and 't2=sum'.
STEP 8: Calculate 'sum=t1+t2' and repeat step 6.
For generating the Fibonacci series, we have to use the below C programming concepts. we recommend learning those for a better understanding
#include <stdio.h>
int main()
{
int t1 = 0, t2 = 1, sum = 0, num; /* declaring the variables sum and num */
printf("Enter an integer: "); /* enters and reads a number from user */
scanf("%d", & num);
printf("Fibonacci Series: %d,%d,", t1, t2); /* now printing first two numbers in Fibonacci series */
sum = t1 + t2;
while (sum < num)
{
printf("%d,", sum);
t1 = t2; /* checking and displaying the Fibonacci numbers upto number user inputs */
t2 = sum;
sum = t1 + t2;
}
} /* main ends */
Enter the limit to generate the Fibonacci sequence 10 Fibonacci sequence is ... 0,1,1,2,3,5,8