Fibonacci Series using the iterative method
The following program prints the Fibonacci series.The first program is implemented using iterative method. The Fibonacci series is a series of numbers in which the current number is the sum of two previous numbers. This program is coded using the C language.
Ex: 0,1,1,2,3,5,8,13....
Description:
The above program prints the number of terms in Fibonacci Series. Here n is the variable which is user input. It acts as a boundary limit for the generated Fibonacci Series.
Output:
Case 1:
Enter the number of terms : 7
The Fibonacci series is :
0 1 1 2 3 5 8
Case 2:
Enter the number of terms : 3
The Fibonacci series is :
0 1 1
Case 3:
Enter the number of terms : 1
The Fibonacci series is :
0
Ex: 0,1,1,2,3,5,8,13....
#include<stdio.h>
int main()
{
int n,a=0,b=1,c,i;
printf("Enter the number of terms : ");
scanf("%d",&n);
printf("\nThe Fibonacci series is :\n");
if(n==0)
return 0;
else if(n==1)
printf("%d",a);
else
{
printf("%d %d ",a,b);
for (i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
}
return 0;
}
Description:
The above program prints the number of terms in Fibonacci Series. Here n is the variable which is user input. It acts as a boundary limit for the generated Fibonacci Series.
Output:
Case 1:
Enter the number of terms : 7
The Fibonacci series is :
0 1 1 2 3 5 8
Case 2:
Enter the number of terms : 3
The Fibonacci series is :
0 1 1
Case 3:
Enter the number of terms : 1
The Fibonacci series is :
0
Comments
Post a Comment