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 :

    1. Input n .
    2. Print 0 and 1
    3. temp = t1 + t2
    4. printf("%d" , t2)
    5. t1=t2
    6. t2=temp
    7. Repeat steps 4 to 6 until i < n
    8. 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

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.