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