Fibonacci Series

Fibonacci Series



The logic for Fibonacci series very simple. It starts with 0 and 1. The next subsequent term is the sum of the previous two terms.

Source Code



// Program for Fibonacci Number

#include <stdio.h>

void main()
{
      int f1 = 0, f2 = 1, f3, n;
      printf("Program for Fibonacci Series\n");
      printf("Enter the maximum number for Fibonacci Series: ");
      scanf("%d", &n);
      printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
      printf("%d\n%d\n", f1, f2);
      while(1)
      {
            f3 = f1 + f2;
            if(f3 > n)
                  break;
            printf("%d\n", f3);
            f1 = f2;
            f2 = f3;
      }
}

 

Output



Program for Fibonacci Series
Enter the maximum number for Fibonacci Series:
Printing Fibonacci Series from 0 - 1000
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987

7 comments:

  1. #include
    #include
    main()
    {
    int a=0, b=1,no,counter,c;
    printf("\nENTER LENGTH OF SERIES (N) : ");
    scanf("%d",&no);
    printf("\nFIBONACCI SERIES");
    printf("\n\t%d %d",a,b);
    for(counter = 1; counter <= no-2; counter++)
    {
    c=a + b;
    printf(" %d",c);
    a=b;
    b=c;
    }
    printf("\n");
    return 0;
    }

    ReplyDelete
    Replies
    1. What is this Md.Faizul Haque Nayan ? Did you read the problem description ?
      I think their is an inappropriateness between your code and the given description.
      Please check the output of your code.

      Best of Luck.....

      Delete
  2. #include
    #include

    main()
    {
    long long num1=0, num2=1, temp1=0, input;

    printf("Enter the bound : ");
    scanf("%lld", &input);

    printf("The Fibonacci series from %lld to %lld is :\n%8lld\n%8lld\n", num1, input, num1, num2);
    for(;;)
    {
    temp1 = num1 + num2;

    num1 = num2;
    num2 = temp1;
    if(temp1 >= input)
    break;

    printf("%8lld\n", temp1);
    }

    getch();
    return 0;
    }

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

    ReplyDelete
  4. // write a program in fibonacci series by given numbers //

    #include
    int main()
    {
    int i,fibo[1000];
    fibo[0]=0;
    fibo[1]=1;
    for(i=2; i<1000; i++){
    fibo[i]= fibo[i-1] + fibo[i-2];
    }
    printf("1st 1000 fibonacci numbers:\n\n");
    for(i=0; i<1000; i++){
    printf("%d\n",fibo[i]);
    }
    return 0;
    }

    ReplyDelete
  5. #include
    main()
    {
    int m=0,n=1,sum=0,i;
    printf("fibonacci serise :\n");
    printf(" %d \n %d",m,n);
    for(i=0;i<=14;i++)
    {
    sum=m+n;
    printf(" \n %d\n",sum);
    m=n;
    n=sum;
    }
    }

    ReplyDelete
  6. #include

    void main()
    {
    int a1 = 0, fa2 = 1, a3, n;
    printf("Program for Fibonacci Series\n");
    printf("Enter the maximum number for Fibonacci Series: ");
    scanf("%d", &n);
    printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
    printf("%d\n%d\n", a1, a2);
    while(1)
    {
    a3 = a1 + a2;
    if(a3 > n)
    break;
    printf("%d\n", a3);
    a1 = a2;
    a2 = a3;
    }
    }

    ReplyDelete