 # C Program to generate Fibonacci series up to given number

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

## What is a Fibonacci series?

Here 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 calculate 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. We have the latest significant Fibonacci number generated. If the 'Sum' is greater than the 'num', we have to stop generating the series. We take a simple example to understand more about this.

Let 1,1,2,3,5,8,13,21,34,...... be the series.

## How to implement the Fibonacci series in C?

In this, the next number is the Summation of the previous numbers, which means 3rd number 2 is the Summation of the first two numbers(1 and 1); similarly, the 4th number (3) is the Summation of the Second and Third numbers(1 and 2). Again, the remaining numbers are also in this series. This type of number or sequence is called the Fibonacci sequence.

The main logic of this program is 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'.Then make the next number as the Sum of 't1' and 't2'.By using a `while loop`, check whether the Sum.

### ALGORITHM

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.

STEP 7: Then Assign 't1=t2' and 't2=sum'.

STEP 8: Calculate 'sum=t1+t2' and repeat step 6.

## C Source Code

```                                          ```#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 */```
```

## OUTPUT

```Enter the limit to generate the Fibonacci sequence
10

Fibonacci sequence is ...
0,1,1,2,3,5,8
```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 