Updated March 16, 2023
Introduction to Fibonacci Series in C
In the Fibonacci Series in C, a number of the series is the result of the addition of the last two numbers of the series. C program with a loop and recursion for the Fibonacci Series. You can print as many series terms as needed using the code below. The Fibonacci numbers are referred to as the numbers of that sequence. For example, the series ‘ first number is 0, 1, 2, 3, 5, 8,… Each other word is the sum of the two preceding terms with the exception of the first two sequence terms, such as 10 = 2 + 8 (addition of the 2 and 8).
The recurrence relationship describes the Fn of Fibonacci numbers in mathematical terms.
Fn = Fn-1 + Fn-2
Examples of Fibonacci Series in C
Below are the examples:
Example #1 – Without Recursion
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 2; i < number; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
return 0;
}
Output:
Example #2 – Using Recursion
Code:
#include<stdio.h>
#include<conio.h>
void printFibonacci(int number)
{
static int first_number = 0, second_number = 1, third_number;
if(number > 0)
{
third_number = first_number + second_number;
first_number = second_number;
second_number = third_number;
printf("%d ",third_number);
printFibonacci(number - 1);
}
}
int main()
{
int number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number: \n");
printf("%d %d ", 0, 1);
printFibonacci(number - 2); //number-2 is used because we have already print 2 numbers
return 0;
}
Output:
Example #3 – Using a For Loop
In the For loop, the Initialization step is executed and only once in the whole program. In this step, you can initialize and declare variables for the code. Then the condition will get evaluated.
If the condition is true, then it will execute the code inside the block of For loop. If the condition is false, it will jump to the code after the For loop without executing the for loop code.
After the For loop, the increment statement will be executed. After that again, the condition will be checked. Loop will get executed if the condition is true, and the loop will repeat itself, i.e. the body of the loop, an increment statement, and condition. The For loop ends when the condition is false.
Program to Generate Fibonacci Series using For Loop:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 0; i < number; i++) //loop will starts from 2 because we have printed 0 and 1 before
{
if(i <= 1)
third_number = i;
else
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
}
return 0;
}
Output:
Example #4 – Using While Loop
In the While loop, Base on Condition, the While loop gets executed multiple times.
If the condition is true, then it will execute the code inside the block of While loop. If the condition is false, it jumps to the code after the While loop without executing the while loop code’s; see how we can generate the Fibonacci series Using While Loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number = 0, i = 3, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
while(i <= number)
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
i = i + 1;
}
return 0;
}
Output:
Example #5 – Using an Array
Let f(n) be the n’th term.
f(0)=0;
f(1)=1;
f(n)=f(n-1)+f(n-2); (for n>=2)
Series will be as Follows:
0
1
0 + 1 = 1
1 +1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13
8 + 13 = 21
13 + 21 = 34
21 + 34 = 55
…and so on
Program to Generate Fibonacci Series using Array:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int fibonacci[25], i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
fibonacci[0] = 0;
fibonacci[1] = 1;
for (i = 2; i < number; i++)
{
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
printf("Fibonacci Series for a given number: \n");
for (i = 0; i < number; i++)
{
printf("%d ", fibonacci[i]);
}
return 0;
}
Output:
Example #6 – Using Specified Number
The first two numbers are 0 and 1, and the other numbers in the series are generated by adding the last two numbers of the series using looping. These numbers are stored in an array and will be printed as output.
Program to Generate Fibonacci Series using Specified Number:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i;
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
return 0;
}
Output:
Conclusion
In this article, we have seen how to generate the Fibonacci series in C by various methods. I hope you’ll find this article helpful.
Recommended Articles
This is a guide to Fibonacci Series in C. Here we discuss the introduction to the Fibonacci series, how to use For Loop, While Loop and Specified Number along with some sample code. You may also look at the following articles to learn more –