Sunday, November 28, 2010

C Program For Fibonacci Series

Hello everybody I want to discuss about How to generate a Fibonacci series in C programming.

What is Fibonacci Series?


The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
In mathematics it is defined by recurrence relation.

Program Code


  


//C Program for generate Fibonacci series
#include "stdio.h"
#include "conio.h"
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

Input\Output



59 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. can u write this program without taking 0,1 as initial values for a and b variables

    ReplyDelete
    Replies
    1. No we can't write this code without intiala'n.

      Delete
    2. void main()
      {
      int i,n;
      clrscr();
      printf("\n enter n for how many times generate series");
      scanf("%d",&n);
      printf("\n FIBONACCI SERIES\n");
      for(i=0;i<n;i++)
      {
      cout<<fabo(i)<<" ";
      }
      getch();
      }

      fabo(n)
      {
      if(n==0)
      return 0;
      if(n==1)
      return 1;
      else
      return fabo(n-1)+fabo(n-2);
      }
      }

      Delete
  3. u hav printed 8 no's bt v ned only 6 no's???

    ReplyDelete
  4. if you take
    a=-1
    b=1
    means u don't need to print a,b

    ReplyDelete
  5. wow thanks its totally right :)

    ReplyDelete
  6. dis is wrong dude ;
    when u want 3rd number in the series (dat is 1) you hav to give value of n == 1...

    ReplyDelete
  7. I hope this solves your problem
    #include
    #include
    void main()
    {
    int a,b,c,i,n;
    a=0;
    b=1;
    printf("Enter number of terms of the series to be generated:");
    scanf("%d",&n);
    if(n==1)
    {
    printf("\n FIBONACCI SERIES\n");
    printf("%d",a);

    }
    else if(n==2)
    {
    printf("\n FIBONACCI SERIES\n");
    printf("%d\t%d",a,b);

    }
    else
    {
    printf("%d\t%d",a,b);
    for(i=3;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);

    }
    }
    getch();
    }

    ReplyDelete
    Replies
    1. /* Fibonacci Series c language */
      #include

      main()
      {
      int n,a=0,b=1,c,d;

      printf("Enter the number of terms\n");
      scanf("%d",&n);
      printf("First %d terms of Fibonacci series are :- \n",n);
      printf("%d\n%d\n",a,b);

      for(d=0;d<n-2;d++)
      {
      c=a+b;
      a=b;
      b=c;

      printf("%d\n",c);
      }
      getch();
      }

      Delete
    2. if i enter the no 1 than it will print 0 and 1 both in place of zero
      i think it's not proper output

      Delete
  8. void main()
    {
    int a,b,c,i,n;
    a=0;
    b=1;
    printf("Enter number of terms of the series to be generated:");
    scanf("%d",&n);
    if(n==1)
    {
    printf("\n FIBONACCI SERIES\n");
    printf("%d",a);

    }
    else if(n==2)
    {
    printf("\n FIBONACCI SERIES\n");
    printf("%d\t%d",a,b);

    }
    else
    {
    printf("%d\t%d",a,b);
    for(i=3;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);

    }
    }
    getch();
    }

    OR

    void main()
    {
    int a,b,c,i,n;
    clrscr();
    a=0;
    b=1;
    printf("\n enter n for how many times generate series");
    scanf("%d",&n);
    printf("\n FIBONACCI SERIES\n");
    printf("\t%d\t%d",a,b);
    for(i=0;i<n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);
    }
    getch();
    }

    ReplyDelete
  9. well, the for loop should be
    for(i=1;i<=n-2;i++)

    ReplyDelete
  10. Thanks!!!!!!!!!!!
    void main()
    {
    int a,b,c,i,n;
    clrscr();
    a=0;
    b=1;
    printf("\n enter n for how many times generate series");
    scanf("%d",&n);
    printf("\n FIBONACCI SERIES\n");
    printf("\t%d\t%d",a,b);
    for(i=0;i<n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);
    }
    getch();
    }

    ReplyDelete
  11. this is the best.........

    #include
    #include
    void main()
    {
    int a,b,c,i,n;
    clrscr();
    a=0;
    b=1;
    printf("\n enter n for how many times generate series");
    scanf("%d",&n);
    printf("\n FIBONACCI SERIES\n");
    printf("\t%d\t%d",a,b);
    for(i=1;i<n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);
    }
    getch();
    }

    ReplyDelete
  12. hey can u post flow chart for this !!! plzz

    ReplyDelete
  13. fibonacci series starts with 1 never with 0...

    ReplyDelete
    Replies
    1. I dont know if u right...but what we were taught was starting from zero itself...well maybe u r right....lets see....

      Delete
  14. This comment has been removed by the author.

    ReplyDelete
  15. fibonacci series
    #include
    #include
    void main()
    {
    int i,n,c,a=0,b=1;
    clrscr();
    printf("enter the number");
    scanf("\n%d\n",&n);
    printf("\nFIBONACCI SERIES\n");
    printf(%d\n%d\n",a,b);
    for(i=3;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("%d\n",c);
    }
    getch();
    }

    ReplyDelete
  16. can explain the running of loop?

    ReplyDelete
  17. //This program will print the fibonacci series by using the printf statement only once...have a close look!!!

    fibonacci series
    #include
    #include
    void main()
    {
    int i,n,c,a=0,b=1;
    clrscr();
    printf("enter the number");
    scanf("\n%d\n",&n);
    printf("\nFIBONACCI SERIES\n");

    for(i=0;i<=n;i++)
    {
    printf("%d\n",a);
    c=a+b;
    a=b;
    b=c;

    }
    getch();
    }

    ReplyDelete
    Replies
    1. hi, u have oversimplified...

      the series is 0,1,1,2,3,5,8... while ur program gives
      0,1,2,3,5,8....
      so the third term gets missing...
      but good attempt....

      i think it is best to use
      for(couter=1;counter<=N-2;counter++) loop....
      :-)

      Delete
  18. can anyone tell how to write reverse fabonnaci series

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int i,n,c,a=0,b=1;
      clrscr();
      printf("enter the number");
      scanf("\n%d\n",&n);
      printf("\nFIBONACCI SERIES\n");

      for(i=n;i<n;i--)
      {
      printf("%d\n",a);
      c=a+b;
      a=b;
      b=c;

      }
      getch();
      }

      Delete
  19. It's running successfully.......... thanks..

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. #include

    main()
    {
    int n, first = 0, second = 1, next, c;

    printf("Enter the number of terms\n");
    scanf("%d",&n);

    printf("First %d terms of Fibonacci series are :-\n",n);

    for ( c = 0 ; c < n ; c++ )
    {
    if ( c <= 1 )
    next = c;
    else
    {
    next = first + second;
    first = second;
    second = next;
    }
    printf("%d\n",next);
    }

    return 0;
    }

    ReplyDelete
  22. Hey Frnz I Have Simple Way To Print it And Hope u Like N try it
    so let's begin

    void main(){

    int a,b,i,n;

    clrscr();

    n=10;

    a=0;

    b=1;

    for(i=1; i<=n; i++){

    a=a+b;

    b=a-b;

    printf("%d ",b);


    }

    getch();}

    the value of 'n' Will Varies as the series wants to maximize

    ReplyDelete
  23. the program is wrong.. publish the correct coding;;;;

    ReplyDelete
  24. u have to give for loop as for(i=2;i<n;i++)
    remaining thnks are ok................

    ReplyDelete
  25. /* Fibonacci Series c language */
    #include

    main()
    {
    int n,a=0,b=1,c,d;
    printf("Enter the number of terms\n");
    scanf("%d",&n);
    printf("First %d terms of Fibonacci series are :-\n",n);
    for(d=0;d<n;d++)
    {
    if (d<=1)
    c=d;
    else
    {
    c=a+b;
    a=b;
    b=c;
    }
    printf("%d\n",c);
    }
    getch();
    }

    ReplyDelete
    Replies
    1. Other way without using "if else"

      /* Fibonacci Series c language */
      #include

      main()
      {
      int n,a=0,b=1,c,d;

      printf("Enter the number of terms\n");
      scanf("%d",&n);
      printf("First %d terms of Fibonacci series are :- \n",n);
      printf("%d\n%d\n",a,b);

      for(d=0;d<n-2;d++)
      {
      c=a+b;
      a=b;
      b=c;

      printf("%d\n",c);
      }
      getch();
      }

      Delete
  26. All above programs are starting with 0 and 1, what if we want another values say -1 or other integer ? Try this one !
    #include
    #include
    int main(void)
    {
    int a,b,c,i,n;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("\nEnter the second number:");
    scanf("%d", &b);
    printf("\nHow many times does you want the series:");
    scanf("%d",&n);
    if(n==1)
    {
    printf("\nFibonacci series is:\t%d", a);
    }
    else if(n==2)
    {
    printf("\nFibonacci series is:\t%d\t%d", a,b);
    }
    else
    {
    printf("\nFibonacci series is:\t%d\t%d", a,b);
    for(i=2; i<n; i++)
    {
    c= a+b;
    a= b;
    b= c;
    printf("\t%d", c);
    }
    }
    getch();
    }

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. #include
    main()
    {
    int i,n,a,b;
    printf("enter n terms");
    scanf("%d",&n);
    printf("fibonacci siries\n");
    for(i=0;i<n;i++)
    {
    a=a+b;
    b=a+b;
    printf("%d %d",a,b);
    }
    }

    ReplyDelete
  29. int main()
    {
    int a, b, fib, n=10,i;

    a=0;b=1;

    for (i=0;i<n;i++)
    {
    printf("fib %d\n", a);
    fib = a+b;
    a=b;
    b=fib;
    }
    return 0;
    }

    ReplyDelete
  30. #include
    #include
    void main()
    {
    int a,b,c,i,n;
    a=0;
    b=1;
    printf("Enter number :");
    scanf("%d",&n);
    if(n==1)
    {
    printf("\n fibonacci series\n");
    printf("%d",a);

    }
    else if(n==2)
    {
    printf("\n fibonacci series\n");
    printf("%d\t%d",a,b);

    }
    else
    {
    printf("%d\t%d",a,b);
    for(i=3;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("\t%d",c);

    }
    }
    getch();
    }

    ReplyDelete
  31. Jst Change the for loop



    for(i=0;i<n-2;i++)

    ReplyDelete
  32. #include
    void main()
    { int n,i,a,b,c;
    printf("enter the range");
    scanf ("%d",&n);
    for(i=o;i<=n;i++)
    {c=a+b;
    a=b;
    b=c;
    printf("the fibonaci series is=%d",c)
    }
    getch();
    }

    ReplyDelete
  33. logic is correct and simple but it's not working.

    ReplyDelete
  34. void main()
    {
    int a=-1,b=1,c,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    printf("%d\t",c);
    }
    getch()
    }

    ReplyDelete
  35. #include
    #include
    main()
    {int a,b,i,n;
    a=b=1;
    printf("enter number of times u want to print fibb series");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {if(i<=1)
    printf("%d",1);
    c=a+b;
    a=b;
    b=c;
    printf("%d",c);
    }
    getch();
    }



    it will print

    1 1 2 3 5 7 12 19.....

    ReplyDelete
  36. #include
    void main(){
    int no,x=1,y=1,z,i;
    printf("Enter number :");
    scanf("%d",&no);
    for(i=2;i<no;i++)
    {
    z=x+y;
    x=y;
    y=x;
    }
    printf("%d",Z);
    getch();
    }

    }

    ReplyDelete
  37. This comment has been removed by the author.

    ReplyDelete