Ticker

6/recent/ticker-posts

Fibonacci series Program



Fibonacci series

  • Do-while 
#include <stdio.h>

int main()
{
    int a=0,b=1,c,n;
    printf("Enter the max. value = ");
    scanf("%d",&n);
    printf("%d,%d,",a,b);
    do 
    {
       c=a+b;
       printf("%d,",c);
       a=b,b=c;
    }
    while
    (b<n);
return 0;
}

Output -

Enter the max. value = 5
0,1,1,2,3,5,


  • For loop
#include <stdio.h>

int main()
{
    int i,n,a=0,b=1,c;
    printf("Enter the no. of values = ");
    scanf("%d",&n);
    printf("%d,%d,",a,b);
    for(i=1;i<=(n-2);i++)
    {
       c=a+b;
       printf("%d,",c);
       a=b,b=c;
    }
    return 0;
}

Output -

Enter the no. of values = 5
0,1,1,2,3,

Post a Comment

0 Comments