Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- * Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
- *
- * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- *
- * By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
- */
- int main()
- {
- int fib,sum = 0;
- int a = 0;
- int b = 1;
- while (fib < 4000000)
- {
- fib = a + b;
- if( fib > 4000000 )
- break;
- if( (fib%2) == 0)
- sum += fib;
- printf("%8d + %8d = %8d\t=>\t%d\n", a,b,fib, sum);
- a = b;
- b = fib;
- }
- printf("Sum: %d\n", sum);
- return 0;
- }
Add Comment
Please, Sign In to add comment