Ad

Friday, March 15, 2013

Fibonacci Sequence in C

Fibonacci Sequence in C

The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, .............
The next number is found by adding up the two numbers before it.
  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3),
  • and so on!
Example: the next number in the sequence above would be 21+34 = 55




We can make this series by using C program. Here is the code..............

#include<stdio.h>

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

    return 0;
}

0 comments:

Post a Comment