I'm using C. I expected this condition in the while loop to prevent any even value bigger than 4 million from being summed in my "sum" variable:
while ( (box1 < 4000000) || (box2 < 4000000) || (box3 < 4000000)) {
box3 = box1 + box2;
if (box3 % 2 == 0) /* checks if box is even */
sum += box3; /* if TRUE adds even value to the variable sum */
box1 = box3 + box2;/* exceeded 4 millions on 12th loop */
if (box1 % 2 == 0)
sum += box1;
box2 = box3 + box1;
if (box2 % 2 == 0)
sum += box2;/* then summed undesired value here */
printf("%d\n", sum);
}
It didn't come to my mind at all the possibility of the boxes exceeding 4 millions in the first or second statement within the loop. That happened and it took me a good while till I figure my program was summing one extra value before the while checks the condition again. I hope you to forgive me and have a little patience for making you spend your precious time here but I need to know if experienced programmers know how to avoid such simple, but annoying errors.
If that is true I would be very glad for having an opportunity to learn from better programmers. Thanks