Fibonacci series program in c; Through this tutorial, we will learn how to display Fibonacci series using for loop, while loop, recursion and function in c programs.
Programs and Algorithm to Display Fibonacci Series in C
Let’s use the following algorithm and programs to program to print fibonacci series in c using while loop, for loop, function and recursion:
- Algorithm Of Fibonacci Series
- C Program to Display Fibonacci Series using While Loop
- C Program to Display Fibonacci Series using For Loop
- C Program to Display Fibonacci Series using Recursion
- C Program to Display Fibonacci Series using Function
Algorithm Of Fibonacci Series
Use the following algorithm to write a program to print fibonacci series; is as follows:
- Start Program
- Declare Some variables.
- Initialize the variables.
- Take input number of terms of Fibonacci series to be printed from user.
- Find and Print fibonacci terms of series.
- End Program.
C Program to Display Fibonacci Series using While Loop
#include <stdio.h>
int main()
{
int Number, i = 0, Next, First_Value = 0, Second_Value = 1;
printf("\n Please Enter the Range Number: ");
scanf("%d",&Number);
while(i < Number)
{
if(i <= 1)
{
Next = i;
}
else
{
Next = First_Value + Second_Value;
First_Value = Second_Value;
Second_Value = Next;
}
printf("%d \t", Next);
i++;
}
return 0;
}
The output of the above c program; is as follows:
Please Enter the Range Number: 15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
C Program to Display Fibonacci Series using For Loop
#include <stdio.h>
int main()
{
int Number, Next, i, First_Value = 0, Second_Value = 1;
printf("\n Please Enter the Range Number: ");
scanf("%d",&Number);
/* Find & Displaying */
for(i = 0; i <= Number; i++)
{
if(i <= 1)
{
Next = i;
}
else
{
Next = First_Value + Second_Value;
First_Value = Second_Value;
Second_Value = Next;
}
printf("%d \t", Next);
}
return 0;
}
The output of the above c program; is as follows:
Please Enter the Range Number: 10 0 1 1 2 3 5 8 13 21 34 55
C Program to Display Fibonacci Series using Recursion
#include<stdio.h>
int FibSeries(int);
int main()
{
int Num, i = 0, j;
printf("\n Please Enter upto which you want too print: ");
scanf("%d", &Num);
printf("Fib series\n");
for ( j = 0 ; j <= Num ; j++ )
{
printf("%d\t", FibSeries(j));
}
return 0;
}
int FibSeries(int Num)
{
if ( Num == 0 )
return 0;
else if ( Num == 1 )
return 1;
else
return ( FibSeries(Num - 1) + FibSeries(Num - 2) );
}
The output of the above c program; is as follows:
Please Enter upto which you want too print: 10 Fib series 0 1 1 2 3 5 8 13 21 34 55
C Program to Display Fibonacci Series using Function
#include<stdio.h>
void Fibseries(int Number) ;
int main()
{
int Number;
printf("Enter the number of terms\n");
scanf("%d", &Number);
printf("First %d :\n", Number);
Fibseries(Number) ;
return 0;
}
void Fibseries(int Number)
{
int i, First_Value = 0, Second_Value = 1, Next;
for(i = 0; i <=Number; i++)
{
if(i <= 1)
{
Next = i;
}
else
{
Next = First_Value + Second_Value;
First_Value = Second_Value;
Second_Value = Next;
}
printf("%d\t", Next);
}
}
The output of the above c program; is as follows:
Enter the number of terms 10 First 10 : 0 1 1 2 3 5 8 13 21 34 55