Printing numbers 1-100, except when divisible by 5 or 12

4.6k views Asked by At

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.

2

There are 2 answers

4
Joop Eggen On

This is what you intended to do, println(a) in all other cases.

for (int a = 1; a <= 100; a++) {
    if (a % 5 == 0) {
        println(a / 5);
    } else if (a % 12 == 0) {
        println(a / 12);
    } else {
        println(a);
    }
}
0
Luke Willis On

Instead of solving for the specific case, let's write an extensible solution. First, what variables are there in the question that could change? I'd say they are:

  1. The starting number 1
  2. The last number 100
  3. The divisors 5 and 12

Additionally, as @Anoml pointed out, the problem wording suggests that if your number is divisible by multiple divisors, we should print out the number of times it is divisible for every applicable divisor. And, as you noted in your question, we only want to print the number itself when it is not divisible by any divisors.

So, our solution becomes:

import acm.program.*;

public class FiveTwelve extends ConsoleProgram {
    private final int FIRST = 1;
    private final int LAST = 100;
    private final int[] DIVISORS = { 5, 12 };

    public void run() {
        boolean hasDivisor;

        for (int i = FIRST; i <= LAST; i++) {
            hasDivisor = false;

            for (int divisor : DIVISORS) {
                if (i % divisor == 0) {
                    println(i / divisor);
                    hasDivisor = true;
                }
            }

            if (!hasDivisor) {
                println(i);
            }
        }
    }
}

This way, we can change our starting number, last number, or which divisors we support, and the program will still pass.