Question about for loops

Correct the following code to add multiples of 3 between 1 and 100. Will this work? Yes or No. If no, explain why.

sum = 0;
for (i = 0; i >= 100; i = i+3)
sum=sum+1


Here is my attempt at fixing the code:

1
2
3
4
5
sum = 0;
for (counter = 3; counter <= 33; counter++)
{	
	sum = sum + counter;
}


I'm thinking maybe the answer is no, it will not work, because it needs to be some other kind of loop, but I'm not sure which. Please help?
Look at the original example. Does i count every multiple of 3 between 1 and 100?
No.

It should be something like

1
2
3
sum = 0;
for (i = 0; i <= 100; i = i+3)
sum=sum+3


But I'm really not sure about the sum = sum + 3 part..


I figured it out!

1
2
3
sum = 0;
for (i = 0; i <= 100; i = i + 3)
	sum = sum + i;

Thank you so much for your help!
Last edited on
Topic archived. No new replies allowed.