1. 程式人生 > >例4-11 斐波那契數列輸出

例4-11 斐波那契數列輸出

with ces -- tdi 輸出 %d 語句 cond clu

例4-11 斐波那契數列輸出

1 1 2 3 5 8……
程序核心——for語句

程序

#include<stdio.h>
int main()
{
    int i,x1=1,x2=1,x;
    printf("%d %d ",x1,x2);
    for(i=1;i<=8;i++)
    {
        x=x1+x2;
        printf("%d ",x);
        x1=x2;
        x2=x;
    }
    printf("\n");
    return 0; 
 } 

結果

1 1 2 3 5 8 13 21 34 55

--------------------------------
Process exited after 0.3538 seconds with return value 0
請按任意鍵繼續. . .

分析

重點:次數循環for

例4-11 斐波那契數列輸出