For loop

i'm trying to write a program that will do this:


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


this is how my program looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
double three, five;
int sumfive, sumthree, sum;
int main()
{
	

	for(int a = 999; a = 0; a--)
	{
		
		three = a%3;

		if (three = 0)
	{
		sumthree += a;
		
	}
	
	
		five = a%5;
		

		if (five = 0)
	{
		sumfive += a;
		
	}
	
	
	}

	sum = sumfive + sumthree;

	std::cout << sum << std::endl;

	system("pause");
	return 0;
}



i can't find any error but for some reason sum is always = 0


i can get it to work by chaning to a while-loop however it outputs the wrong number when using the same code but instead of a for loop i use


1
2
3
while (a < 1000)
(same code as above)
a++;



anyone got an idea? :<
Last edited on
@Serri

You made a small error in your for loop.
Instead of..
for(int a = 999; a = 0; a--)
it should be..
for(int a = 999; a > 0; a--)
change line 13 to if (three == 0)
change line 23 to if (five == 0)

Also initialize sumfive and sumthree to 0. Even though your compiler may set it to zero, I don't think that this behavior is defined.
In this case, it is well defined. The variables must be set to 0 by a conforming compiler. Not initializing them explicitly, however, is a bad habit to get into and so is making all variables global.
thanks for the help guys it now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include "windows.h"
double three, five;
int sumfive = 0, sumthree = 0;
int main()
{
	
	for(int a = 999; a > 0; a--)
	
	{
		
		three = a%3;

		if (three == 0)
	{
		sumthree += a;
		
	}
	
		five = a%5;
		

		if (five == 0)
	{
		sumfive += a;
		
	}
	




	}

	int sum = sumfive + sumthree;
		
	std::cout << sum << std::endl;
	system("pause");
	return 0;
}


however it outputs the wrong answer.
supposedly the sum of all the multiples of 3 or 5 below 1000 is 233168 my program outputs 266333.
Last edited on
Try testing, with a smaller value like 50, use cout statements in the loops to print out intermediate values, so you can see what's going on.

Even better, learn how to use a debugger, - it's fairly easy with an IDE.

HTH
> supposedly the sum of all the multiples of 3 or 5 below 1000 is 233168 my program outputs 266333.

You are adding up multiples of 15 twice.

Let a be the sum of multiples of 3 below 1000
a == 3 + 6 + ..... + 999 == 3 * ( 1 + 2 + ... + 333 ) == 3 * 333 * 334 / 2

Let b be the sum of multiples of 5 below 1000
b == 5 + 10 + ..... + 995 == 5 * ( 1 + 2 + ... + 199 ) == 5 * 199 * 200 / 2

Let c be the sum of multiples of 15 below 1000
c == 15 + 30 + ..... + 990 == 15 * ( 1 + 2 + ... + 66 ) == 15 * 66 * 67 / 2

The sum of all the multiples of either 3 or 5 or both below 1000 == a + b - c

The sum of all the multiples of either 3 or 5 but not both below 1000 == a + b - c*2
I think i get what you're saying.. since the problem was to solve multiples of 3 OR 5

only one of them should be added each time 3*x or 5*x becomes the same value

so for example

1
2
3
4
a = 3 + 6 + 9 + 12 + 15

b = 5 + 10 + 15


both are now at 15, allthought only one of the should be used, since the question was "or" not "and", correct?
> only one of them should be added each time 3*x or 5*x becomes the same value

Yes.
Topic archived. No new replies allowed.