Monday, 11 August 2014

Create Fibonacci sequence using C language

Fibonacci sequence is the number with following sequence
0,1,1,2,3,5,8,13,21,34,55,89,144……
Where every number is the sum of his previous two numbers. For example find any number in series, take its position as “f” now value of “f” can be calculated by
f = (f-1) + (f-2)
Now let’s write the code for it
#include<stdio.h>
void main()
{
    int n,a=0,b=1,i,c=0;
    printf("Enter the range of your number ");
    scanf("%d",&n);
    printf("Your series is \n");
    printf("%d %d ",a,b);
    for(i=2;i    {   c=a+b;
        printf("%d ",c);
        a=b;
        b=c;
    }
}
In above program first two value is constant, for that we print them differently. The we create a “for loop” which will give other values. Now let’s see the output
Enter the range of your number 10
0 1 1 2 3 5 8 13 21 34

0 comments:

Post a Comment