C Program – Ways to Generate Fibonacci Series

By | 2017-08-19T14:03:24+00:00 August 19th, 2017|c-program|0 Comments

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; 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

[/box]

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)); 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

[/box]

Happy Learning 🙂

About the Author:

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+ ,

Leave A Comment