Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get 100 integers but exclude some numbers?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading