Find the sum of the multiples of 2 under 547
Lets break this down...
A "Multiple of two" is a number that can be divided by two evenly. Meaning there is no remainder. Look at this link for more info.
Ok we know what a "Multiple of two" is now lets look at the sum...
We want to add up "Multiples of two" under 547. Meaning we will count up to 547 by two and add them together.
so 2 + 4 + 6 + .... + 544 + 546 = A big number
Like @Mureinik posted...we start by defining and initializing something to hold our sum (adding up all the counting by two) in.
long sum = 0;
Then we need to count up to 547 by twos.
for(int i = 0; i < 547; i += 2){
}
This is a for
loop. It will execute what is inside the { }
until the condition is false. It defines i
as the integer of 0. The condition is i < 547
. It will also increment i
by two each time through the loop. That is what the i += 2
part does. (i += 2
is the same thing as i = i + 2
)
So now we have something to hold our summation in (sum
) and we have a means of counting by two (the for
loop). All we have to do is add up the numbers.
sum += i;
This will take care of that for you. Like you hopefully have guessed sum += i
is short hand for sum = sum + i;
. i
will always be a "Multiple of two" because we are always adding 2 to i
. Once i
gets to 548 it will fall out of the loop and stop adding up the sum
.
Like others have commented it will not hurt to look up some tutorials on the web. I know that it can be over whelming you just have to stick with it.
Hope this clears up some stuff for you. Code On.