# sum of fibonacci

• 08-18-2005
bazzano
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); }```
• 08-18-2005
quzah
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.
• 08-18-2005
bazzano
thanks for that i got it