Search This Blog

Generate the Fibonacci sequence up to N | c programming

Generate the  Fibonacci  sequence up to N

 

problem

Read a integer value N. Print the fibonacci sequence up to N.







Solution

The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21........



#include<stdio.h>

int main()
{
    int N,i,next,f,s;
    scanf("%d",&N);

    f = 0;
    s = 1;
    for(i=0;i<=N;i++){
        if(i<=1){
            next = i;
        }
        else{
            next=f+s;
            f=s;
            s=next;
        }

            printf("%d,",next);

    }
    return 0;
}
 

No comments

Powered by Blogger.