I have a task: I need to number 100 apartments in a building, but they should not have numbers 3 and 5 (13, 15, 23, 25 and so on). So there will be apartments with numbers 101, 104 etc. The problem is I should only use cycles (for or while) and if statement to do it.
int counter = 0;
for (int i = 1; i < 100; i++) {
if (i / 10 == 3 || i % 10 == 3 || i / 10 == 5 || i % 10 == 5) {
continue;
}
System.out.print(i + " ");
counter++;
}
System.out.println(counter);
It's clumsy, and I have only numbers UP TO 100 and not 100. Overall, I now have only 63 apartments numbered
You should not test condition by i
but counter
:
int counter = 0;
for (int i = 1; counter < 100; i++) {
if (i / 10 == 3 || i % 10 == 3 || i / 10 == 5 || i % 10 == 5) {
continue;
} else {
++counter;
System.out.println(counter + ": " + i);
}
}
P.S. I used the if condition as is, but it has another problem.