# Thread: sum of multiples of 3 or 5 below 1,000

1. ## sum of multiples of 3 or 5 below 1,000

I wrote a program which should compute the sum of all of the multiples of 3 or 5 below 1,000.
Code:
```int main()
{
int sum = 0;
int i;

for (i=0; i < 1000; i++)
{
if (i % 3 == 0)
sum += i;

if (i % 5 == 0)
sum += i;
}

printf(" \n\n%i\n\n", sum);

return 0;
}```
This program produces 266,333. According to the internet the correct answer is 233,168 which I verified in another program in ruby. Any thoughts as to why I am getting this incorrect result?

2. Your if statements are not mutually exclusive, so you count numbers that are multiples of both 3 and 5 twice, e.g. 15, 30, ....

3. You are double counting. Try i = 15, for example.

4. Like this...

Code:
```int main( void )
{
int sum = 0;
int i;

for (i=0; i < 1000; i++)
{
if (i % 3 == 0)
sum += i;
else if (i % 5 == 0)
sum += i;
}
printf(" \n\n%i\n\n", sum);
return 0;
}```

5. Personally, I would write that part as:
Code:
```if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}```

6. Ahh, I see. Thank you all.

7. My pleasure...