1

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.

Fulgie
  • 11
  • 1
  • 3
  • 2
    Big hint: `println(a);` is not restricted by any condition so it always execute. Try to explain where should it be executed and where not. Then try to code it that way. – Pshemo Oct 31 '17 at 19:06
  • 1
    Note: also think about the time that 'a' is divisible by both 5 and 12. This would be missed because you use an "else if" – Airo Oct 31 '17 at 19:14
  • Not to get too mathematical but [this guide](https://artofproblemsolving.com/wiki/index.php/Modular_arithmetic/Introduction#Multiplication) will help you understand exactly what @Anoml is saying. If you're up for the challenge, you may explore and even try proving their [properties](https://en.wikipedia.org/wiki/Modular_arithmetic#Properties) – eshirima Oct 31 '17 at 19:37

2 Answers2

3

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.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
1

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);
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    Don't forget the case of a=60, it is divisible by 5 and 12. – Airo Oct 31 '17 at 19:16
  • @Anoml spoil sport, but right. OP: always ask yourself why they build in two conditions. – Joop Eggen Oct 31 '17 at 19:21
  • @JoopEggen this is a traditional fizzbuzz question, you would print both conditions. – logger Oct 31 '17 at 19:26
  • Thanks for actually realising my intentions. This was my purpose by starting but as others mentioned there are examples like number 60 which is divisible by both 5&12.The example of yours should be my choice for an edit because Im currently a beginner and dont have much knowledge over previous answers. – Fulgie Nov 01 '17 at 10:06