Write a program that uses a loop to add up all the multiples of three from 1 to 100. Challenge: write three programs, each using a different type of loop. (Hint: If you want to check if a number is a multiple of three, you might need to use an if statement. Also, if a number n % 3 is equal to zero, what does that mean?)
top of page
bottom of page
class Main {
public static void main(String[] args) {
int x = 0;
for (int y=0; y<100; y++) {
if (y%3 == 0) {
x = x + y;
}
}
System.out.println(x);
}
}
class Main { public static void main(String[] args) { int a = 0; for (int x = 0; x<100; x++){ if (x % 3 == 0) { a = a + x; } } System.out.println(a); } }