Thread: sum of fibonacci

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    sum of fibonacci

    Hi guys having more trouble with my fibonacci sequence i am able to get user input and display the correct sequence for example: The user may enter 15

    The program will print out: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

    So that part is correct but how would i add the sum up of everyone of those fibonacci numbers. Assuming it would add up to 986 the way im calculating it must be incorrect or where im putting the variables because im getting a totally different figure.






    Code:
    void fib()
    {
    
       int number;
       int a = 0 ;
       int i=0;
       char buff [MAXLINE];
       int b = 1;
       int c = 0;
    
    
    
       printf("Enter a positive Integer!");
    
       fgets(buff,MAXLINE,stdin);
    
       do
       {
         fgets(buff, MAXLINE, stdin);
         number = atoi(buff);
    
       if(number >0 && number <40)
       {
    
          printf("You have entered the correct input!\n\n");
          printf("Fibonacci sequence for the first %d terms:\n\n", number);
    
    
          printf("%d%   d",a,b);
    
          for(i = 2; i<number; i++)
          {
            c = a + b;
            printf("\t%d",c);
            a=b;
            b = c;
    
    
    
    
          }
          c = a + b;   /* over here i am trying to get the sum of every fibonacci
                       number calculated but im doing something wrong even
                      with the average where should the variables be calculated
                      in or outside the for loop  */
          printf("Sum: %d \n", c);
    
          
    
       }
       else
       {
    
        printf("invalid input please try again!");
       }
    
      }
       while(number <=0 || number >40);
    
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'd sum it up the same way you'd sum up anything else.
    Code:
    int x, sum = 0;
    for( x = 0; x < 10; x++ )
    {
        printf("we're printing x, so now let's sum it too... %d\n", x );
        sum += x;
    }
    printf("The sum is %d.\n", sum );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    thanks for that i got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM