The question is
Write a Java program that prints numbers from 1 to 100. If the number is divisible by 5, instead of printing the number, your program should print how many fives are in this number, if the number is divisible by 12 it should print how many twelves are in this number.
Here's my current attempt:
import acm.program.*;
public class FiveTwelve extends ConsoleProgram {
public void run() {
int a = (1);
int b = (5);
int c = (12);
for (a = 1; a <= 100; a++) {
println(a);
if ((a % 5) == 0) {
println(a/b);
} else if ((a % 12) == 0) {
println(a / c);
}
}
}
}
The problem is that my code is also printing the divisible number instead of just the outcome. Example output:
1
2
3
4
5
1
6
7
8
9
10
2
11
12
1
This goes on. I want to remove numbers that are divisible by 5 and 12. Instead, I need to show the result or num/5 && num/12.
UPDATE!!!
import acm.program.*;
public class FiveTwelve Extends ConsoleProgram{
public void run() {
for (int a = 1; a <= 100; a++) {
if (a % 5 == 0 && a % 12 == 0) {
println(a / 5);
println(a / 12);
} else if (a % 5 == 0) {
println(a / 5);
} else if (a % 12 == 0) {
println(a / 12);
} else {
println(a);
}
}
}
}
guess we all missed something.