Guest User

Untitled

a guest
Nov 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. /*
  3. * 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:
  4. *
  5. * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  6. *
  7. * By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  8. */
  9.  
  10. int main()
  11. {
  12. int fib,sum = 0;
  13.  
  14. int a = 0;
  15. int b = 1;
  16.  
  17.  
  18. while (fib < 4000000)
  19. {
  20.  
  21. fib = a + b;
  22. if( fib > 4000000 )
  23. break;
  24. if( (fib%2) == 0)
  25. sum += fib;
  26.  
  27. printf("%8d + %8d = %8d\t=>\t%d\n", a,b,fib, sum);
  28.  
  29. a = b;
  30. b = fib;
  31. }
  32.  
  33. printf("Sum: %d\n", sum);
  34. return 0;
  35. }
Add Comment
Please, Sign In to add comment