4

I'm trying to write a program that can multiply all the digits of a number from 0 to 1000 exclusive using only math expressions in Java. My program works fine as long as the user types in a 3-digit number, but results in 0 if they type in anything less than 100.

I have tried getting the last digit of the input with '%10' and removing the last digit with '/10' but without a control statement to detect if the input has been reduced to zero, the program ends up multiplying by 0 when a 2-digit number has been reduced to zero, giving an incorrect result.

public class MultiplyDigits {
    public static void main(String[] args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000: ");
        int number = input.nextInt();
        int product = 1;
        product*=number%10;
        number/=10;
        product*=number%10;
        number/=10;
        product*=number%10;
        System.out.println(product);
    }
}

An input of 55 should result in 25, but my program does 5 x 5 x 0 = 0

An input of 999 results in 729, which is correct. 9 x 9 x 9 = 729

Some more clarification, this is a problem out of the 2nd chapter of a textbook for complete novices. The author has not covered selection statements, loops, writing our own methods or classes, or anything more advanced than elementary programming, so the implication is that this is doable without those. The book has covered invoking methods in classes built into Java, although the author has only mentioned methods in the Math and System classes. For example, Math.max(), Math.min(), Math.pow(), System.currentTimeMillis();

John Mulligan
  • 43
  • 1
  • 5

4 Answers4

4

What about this variant. To find the first number, you can decrease, first of all, the entered number by 100 and add 1 to avoid 0 during multipication. And , as recomended NVioli, the second number should be the same updated to have a possibility to enter number lower then 10. Thus, the final variant is:

int number = input.nextInt();

    int t1 = 1 + (number-100) / 100;
    int t2 = (1 + (number-10) / 10) % 10; \\By NVioli
    int t3 = number % 10;

    int  product = t1 * t2 * t3;

    System.out.println(product);
Sergei Voychuk
  • 171
  • 2
  • 8
0

The first part is to extract the essential code into a separate Java method. I'm calling it dprod, which is short for "digit product".

static int dprod(int x) {
    int hun = x / 100 % 10;
    int ten = x / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

The above code is the naive version that only works for numbers >= 100.

To treat numbers less than 100 as expected, you need to replace the hun or ten with 1 if it is 0.

static int dprod(int x) {
    int hun = x < 100 ? 1 : x / 100 % 10;
    int ten = x < 10 ? 1 : x / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

The ?: operator is called a conditional operator, therefore it is probably not allowed under your rules. There is a possible workaround by using the ?: operator without writing it explicitly, by using the Math.max function.

static int dprod(int x) {
    int hun = Math.max(100, x) / 100 % 10;
    int ten = Math.max(10, x) / 10 % 10;
    int one = x / 1 % 10;

    return hun * ten * one;
}

The Math.max function uses the ?: operator internally, therefore it might be forbidden, too. This is subject to discussion though, since it depends on the exact wording of the rules and their intention.

If Math.max is forbidden, it is possible to implement it entirely without branches or conditions, see this C++ question, which can be translated to Java by replacing int32 with int and by replacing inline with static.

The complete code, including automatic tests, is:

package de.roland_illig.so;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class DprodTest {

    static int dprod(int x) {
        int hun = Math.max(x, 100) / 100 % 10;
        int ten = Math.max(x, 10) / 10 % 10;
        int one = x / 1 % 10;

        return hun * ten * one;
    }

    @Test
    public void testDprod() {
        assertThat(dprod(999)).isEqualTo(729);
        assertThat(dprod(123)).isEqualTo(6);
        assertThat(dprod(99)).isEqualTo(81);
        assertThat(dprod(9)).isEqualTo(9);
    }
}
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

You could just initialize the program with a length 1000 array, initialize it with the value of each number, and then your real problem simplifies to:

System.out.println(calculatedArray[number]);

Your initialization could even take advantage of the fact that a leading 0 doesn't matter according to your rules (55 and 155 are the same result.)

calculatedArray[55] = calculcate(155);
Steve
  • 692
  • 1
  • 7
  • 18
  • 1
    Admittedly, this is a ludicrous solution ... but no more ludicrous than the arbitrary requirement that you are not allowed to use an if statement to account for 0. – Steve Feb 06 '19 at 21:35
-1

there are some ways which can help you but all of them has a simple loop or if:

  1. You can use digits = Logarithm of your number(10 base) and then you have number of digits, then you can use a loop to calculate the result. your loop will be repeated digit times so no matter how many digits your number has, it will always work.

  2. You can check if your number is less than 100 and then just add 100 to that, then calculate the result, because of 1 * digit1 * digit2 there will be no error.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28
  • Read the title of the question: **...without using control statements or loops**. – forpas Feb 06 '19 at 18:38
  • @forpas and you can see`all of them has a simple loop or if:`, i believed it may help him even though its not the asnwer – Amir Hedieh Feb 06 '19 at 18:39
  • The simplest solution is what the OP has already, but it does not solve his problem because it does not meet the requirements, so your answer will not help at all since it is more complex and it also does not meet the requirements. – forpas Feb 06 '19 at 18:42
  • Because you end up wanting to treat 0 different than the other digits (you want to treat a zero like a 1), I'm not sure there is possible without some sort of control statement. – Blair Feb 06 '19 at 19:01