The statement
e=e%2;
will result in e being equal to the remainder of the division of e by 2. In other words, if e is even, e will become 0, if e is odd, e will become 1, and e will not change after that no matter how many times you loop.
To display the sum of all even numbers, you will need TWO variables, one for the sum and one for the even numbers. You should loop according to the even numbers until you get to n, add add them up using the sum.
Have a go and let us know if you need more help.
Cheers
darkagn
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
Yes dougy83 that is what i was getting at, but I didn't want to give away the answer. :P
Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
int e;
for (e=2; e<=n; e+=2)
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
That's incorrect. It was only required in C89/90. C++ never had such requirement. Even C99 removes this restriction.
I stand corrected - apologies.
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200