About Fibonacci sequence, using the C language output

Fibonacci series introduction:

It has the following characteristics: 1,2 1,1 number two. From the third number, which is the number and the sum of the previous two.

Example: 1,1,2,3,5,8,13,21, ...

Use ordinary cycle:

! ! ! Problems require: a list of twenty the number of columns ago

#include <stdio.h>
int main(){
    int f1=1;
    int f2=1;
    printf("%d %d\n",f1,f2);
    for(int i = 3;i < 12;i++){
        f1=f1+f2;
        f2=f2+f1;
        printf("%d %d\n",f1,f2);
    }
    return 0;
}

Using an array of methods:

#include <stdio.h>
int main(){
    int a[20]={1,1};
    printf("%d\n%d\n",a[0],a[1]);
    for(int i = 2;i < 20;i++){
        a[i]=a[i-1]+a[i-2];
        printf("%d\n",a[i]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/jcfeng/p/11233402.html
Recommended