# Fibonacci Series in C

July 06, 2021

The Fibonacci Sequence is the series of numbers following the rule that each number, called a Fibonacci number is equal to the sum of the two preceding numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

• the 2 found by adding two numbers before it (ie., 1+1).
• the 3 found by adding two numbers before it (ie., 2+1).
• so on...

In this example, you will learn how to display the Fibonacci series in c of first n numbers.

To understand this example, You should have knowledge in

• C Operators
• C for loop
• C while loop and do-while loop
• break and continue statement in C

Fibonacci Series up to n terms

```    #include <stdio.h>
int main() {

int i, num; //num initialize for number of terms

// initialize first and second terms
int a = 0, b = 1;

// initialize the next term (3rd term)
int c = a + b;

printf("Enter the number of terms: ");
scanf("%d", &num);

// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", a, b);

// loop starts from 3. because 1 and 2 are already printed
for (i = 3; i <= num; ++i) {
printf("%d, ", c);
a = b;
b = c;
c = a + b;
}
return 0;
}
```

Output

Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

0 0
July 06, 2021
```    #include<stdio.h>
int main(){
int num1=0,num2=1,num_next,i,limit;
printf("Enter the number of elements:");
scanf("%d",&limit);
printf("\n%d %d",num1,num2);//printing 0 and 1
for(i=2;i<limit;++i){
num_next=num1+num2;
printf(" %d",num_next);
num1=num2;
num2=num_next;
}
return 0;
}
```

Output

Enter the number of elements:10

0 1 1 2 3 5 8 13 21 34

0 0
July 06, 2021

Fibonacci Series in C using loop

One of the ways to display the Fibonacci series in the C programming language is using for loop.
The program prompts to input the number of terms and displays the series of Fibonacci having the same number of terms.

```    #include<stdio.h>
int main()
{
int limit, term_one = 0, term_two = 1, next_term, i;

printf("Enter the number of terms:\n");
scanf("%d",&limit);

printf("Fibonacci series:\n");
for ( i = 0 ; i < limit ; i++ )
{
if ( i <= 1 )
next_term = i;
else
{
next_term = term_one + term_two;
term_one = term_two;
term_two = next_term;
}
printf("%d\n",next_term);
}

return 0;
}
```

Output:

Enter the number of terms:
5
Fibonacci series:
0
1
1
2
3

0 0