当前位置:网站首页>Non recursive printing Fibonacci sequence

Non recursive printing Fibonacci sequence

2022-06-22 05:07:00 Jiangxueqian

#include <stdio.h>

int main()
{
    int n;             // The number of elements of the sequence to be printed 
    int a = 1, b = 1;  // The initial values of the first and second terms of the sequence 
    int c = 0;         // Array element values 

    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
    {
        if (i == 1 || i == 2)
        {
            printf("%-3d\t", 1);
        }
        else
        {
            c = a + b;             // The third is the sum of the first two 
            printf("%-3d\t", c);   // Output the third item 
            a = b;                 // The original second item becomes the new first item 
            b = c;                 // The original third item becomes the new second item 
        }
    }

    return 0;
}

flow chart :

  

原网站

版权声明
本文为[Jiangxueqian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503534535.html