trying to do a program that sums all the even numbers in the sequence but i think im doing it wrong

heres my code

#include <stdio.h>
int fib ( int n );
int main ( int argc, char *argv[ ] ) {


int i , sum = 0 ;

for(i = 1; i <= 6 ;++i) {

int v = fib(i) ;

if( (v % 2) == 0 ) {

sum = sum + i ;

}

}

printf("the sum is %d",sum) ;

return 0 ;

}

int fib(int n) {

if ( n == 1 || n == 2) return 1 ;

else

return fib(n - 1) + fib( n - 2 ) ;

}

Recommended Answers

All 2 Replies

Are you summing the actual values in the sequence or the index within the sequence? Because your code presently does the latter by adding i to the sum instead of v .

yea just noticed that lol thanks it works now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.