# Fibonacci series C program

Fibonacci series in C programming: C program for Fibonacci series using a loop and recursion. Using the code below you can print as many terms of the series as required. Numbers of this sequence are known as Fibonacci numbers. The first few numbers of the series are 0, 1, 1, 2, 3, 5, 8, ...,. Except for the first two terms of the sequence, every other term is the sum of the previous two terms, for example, 8 = 3 + 5 (addition of 3 and 5).

## Fibonacci series C program using a for loop

1. /* Fibonacci series program in C language */
2. #include <stdio.h>
3.
4. int main()
5. {
6.   int n, first = 0, second = 1, next, c;
7.
8.   printf("Enter the number of terms\n");
9.   scanf("%d", &n);
10.
11.   printf("First %d terms of Fibonacci series are:\n", n);
12.
13.   for (c = 0; c < n; c++)
14.   {
15.     if (c <= 1)
16.       next = c;
17.     else
18.     {
19.       next = first + second;
20.       first = second;
21.       second = next;
22.     }
23.     printf("%d\n", next);
24.   }
25.
26.   return 0;
27. }

Output of program:

## Fibonacci series C program using recursion

1. #include<stdio.h>
2.
3. int f(int);
4.
5. int main()
6. {
7.   int n, i = 0, c;
8.
9.   scanf("%d", &n);
10.
11.   printf("Fibonacci series terms are:\n");
12.
13.   for (c = 1; c <= n; c++)
14.   {
15.     printf("%d\n", f(i));
16.     i++;
17.   }
18.
19.   return 0;
20. }
21.
22. int f(int n)
23. {
24.   if (n == 0 || n == 1)
25.     return n;
26.   else
27.     return (f(n-1) + f(n-2));
28. }

The recursive method is less efficient as it involves repeated function calls and there are chances of stack overflow as the function will frequently be called for calculating larger Fibonacci numbers. This sequence has many applications in Mathematics and Computer Science.