Write a program to print Fibonacci series of given range

Write a program to print Fibonacci series of given range

// Write a program to print Fibonacci series of given range          
// Ex: 5-> 1 1 2 3 5; 26-> 1 1 2 3 5 8 13 21

 
#include
int main(){
    int a=1,b=1,n;
    printf("Enter a positive number:");              // input from user
    scanf("%d",&n);
    while(n<=0){
         printf("Please enter a positive number:");              // input from user
         scanf("%d",&n);      
    }
    printf("Fibonacci series: %d %d ",a,b);
    while((a+b)<=n){
         b = a+b;
         a = b-a;
         printf("%d ",b);          
    }
    getch();
}

No comments:

Post a Comment