Wednesday, July 30, 2014

how to find out the sum of the fibonacci number ??

how to find out the sum of the fibonacci number ??

first of the simple thing here:

What is the fibonacci number ?

So it is the series start as below:

0 1 1 2 3 5 8 13 21.......

you can find out this one easily: (i run this programme in Dev c++ 4.2.1)

1) for the short number:

#include<stdio.h>
#include<conio.h>
int main()
{
    int x,y,sum,n,i;
    x=0,y=1;
    printf("how much number you want to print:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        sum=x+y;
        x=y;
        y=sum;
    }
    printf("\nthe sum of the fibonacci number is=%d",sum);
   
}

2)

the real logic is come now because what you can think for the large number.because the above programme can only count the sum around the 9 digit only.
you can check this one by the find the sum first 48 number.it will be negative means the answer can not store in the int. so we apply our logic here.



#include<stdio.h>
#include<conio.h>
int main()
{
    int i,k,j=1,temp,a[100]={'0'},b=0,c[100]={'0'},n;
    a[0]=1,c[0]=0;      //initialize the value of the two array
    printf("how many value you want of fibonacci number :");
    scanf("%d",&n);
    for(k=1;k<=n;k++)
    {
    for(i=0;i<=b;i++)
    {
              if(temp>0)       //case 1
         
            {
                j=a[i]+c[i]+temp;
                c[i]=a[i];
            a[i]=j%10;
            temp=j/10;
                //  printf("the value of i=%d a[%d]=%d c[%d]=%d b=%d\n",i,i,a[i],i,c[i],b);  //to understand the process
                }
            else            //case 2
            {
            j=a[i]+c[i]+temp;
            c[i]=a[i];
            a[i]=j%10;
            temp=j/10;
            //    printf("the value of i=%d a[%d]=%d c[%d]=%d b=%d\n",i,i,a[i],i,c[i],b);     //to understand the process
            }
        if(temp>0 && i==b)   //case 3
                {
                a[i+1]=temp;
                temp=0;
                b++;
                i++;
                //    printf("the value of i=%d a[%d]=%d c[%d]=%d b=%d\n\n",i,i,a[i],i,c[i],b);   // to understand the process
                }
       
    }
}
    for(k=b;k>=0;k--)
    {
        printf("%d",a[k]);
    }
}




it will work for any number of digit value. Just increase the value of the two array when needed to store more digit.but logic of array use here is awasome guys..