C PROGRAM FOR FIBONACCI SERIES WITHOUT RECURSION
Consider the following I/O samples.
SAMPLE INPUT: 5
SAMPLE OUTPUT: 0 1 1 2 3
The algorithm used here is :
- Input n .
- Print 0 and 1
- temp = t1 + t2
- printf("%d" , t2)
- t1=t2
- t2=temp
- Repeat steps 4 to 6 until i < n
- Print the output.
C PROGRAM:
#include<stdio.h>
int main()
{
int n, t1 = 0,t2 = 1, temp,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n%d",t1);
for(i=1;i<n;i++)
{
printf(" %d",t2);
temp = t1+t2;
t1=t2;
t2=temp;
}
return 0;
}
OUTPUT:
The given source code in C program "for Fibonacci series without recursion" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment