Monday , November 20 2017
Home / c-program / C Program – Ways to Generate Fibonacci Series

C Program – Ways to Generate Fibonacci Series

C- Program to generate Fibonacci Series.

Fibonacci Series :


#include<stdio.h> 
int main(void) 
{ 
    long a,b,c; 
    int i,n; 
    a=0; 
    b=1; 
    printf("Enter the number of terms : "); 
    scanf("%d",&n); 
    printf("%ld ",b); 
    for(i=1; i<n; i++) 
    { 
        c=a+b; 
        printf("%ld ",c); 
        a=b; 
        b=c; 
    } 
    printf("\n"); 
    return 0; 
}

Enter the number of times : 10
1 1 2 3 5 8 13 21 34 55

Enter number of times : 15
1  1  2  3  5  8  13  21  34  55  89  144  233  377  610

Fibonacci Series Recursive:


#include<stdio.h> 
int fibonacci(int n);         
 
int main(void) 
{ 
    int ntimes, i; 
    printf("Enter number of times : "); 
    scanf("%d",&ntimes); 
     
    for(i=0; i<ntimes; i++) 
        printf("%d  ",fibonacci(i)); 
    printf("\n"); 
    return 0; 
} 
 
int fibonacci(int n)         
{ 
    if(n==0 || n==1) 
        return(1); 
    return(fibonacci(n-1) + fibonacci(n-2)); 
}

Enter the number of times : 10
1 1 2 3 5 8 13 21 34 55

Enter the number of terms : 20
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

C Program to add elements of an array

How to add elements in an array using C Program. Add elements of an Array …

Leave a Reply

Your email address will not be published. Required fields are marked *