I'm having problems with this code. I don't want to look at others, so I'm wondering what's wrong with mine.
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.
public class Multiples {
public static void main (String [] args) {
int temp = 0;
int temp2 = 0;
for (int i = 0; i <= 1000; i++) {
if (i % 3 == 0) {
temp = temp + i;
}
}
for (int j = 0; j <= 1000; j++) {
if (j % 5 == 0) {
temp2 = temp2 + j;
}
}
System.out.println(temp + temp2);
}
}
The value I get is 267333, which is wrong. Is my adding wrong? I know algorithmically, this code might not be up to par, but it should work, right?