Find the Product of the Digits of a Number
Compute the product of all digits of an integer using arithmetic.
Start a product at 1, peel off each digit with number % 10, multiply it into the product, then drop it with number / 10. Repeat until the number becomes 0. If any digit is 0 the whole product becomes 0, which is expected.
Problem Statement
Given an integer, return the product of its digits. For example the digits of
234 are 2, 3, 4, and their product is 24.
Work with the absolute value so negatives behave like their positive form, and
note that any digit equal to 0 makes the entire product 0. Do not convert
the number to a String.
Input: A single integer n.
Output: An integer equal to the product of the digits of n.
Examples
Input: 234
Output: 242 * 3 * 4 = 24.
Input: 405
Output: 04 * 0 * 5 = 0; the zero digit zeroes the product.
Constraints
-2,147,483,648 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- What starting value for a running product leaves the first multiplication unchanged?
- How does one digit equal to 0 affect the whole product?
- How do you extract and then remove each digit?
- What should the product of 0 (the number) be?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Accumulate a product digit by digit.
- Take the absolute value of
n. - If it is 0, the number has the single digit 0, so return 0.
- Set
product = 1. - While the value is not 0:
digit = value % 10— the current last digit.product *= digit— multiply it in.value = value / 10— drop the digit.
- Return
product.
Dry Run
Walk through the example step by step
Multiplying the digits of n = 234:
step | value | digit = value % 10 | product
-----+-------+--------------------+--------
start | 1
1 | 234 | 4 | 4
2 | 23 | 3 | 12
3 | 2 | 2 | 24
end | 0 | — | 24
Solution
Reveal the full Java solution
public class ProductOfDigits {
public static int productOfDigits(int n) {
int value = Math.abs(n);
if (value == 0) {
return 0;
}
long product = 1; // long guards multiplication against overflow
while (value != 0) {
int digit = value % 10;
product *= digit;
value /= 10;
}
return (int) product;
}
public static void main(String[] args) {
System.out.println(productOfDigits(234)); // 24
System.out.println(productOfDigits(405)); // 0
}
}
The accumulator must start at 1 — the multiplicative identity — so the first
digit is preserved rather than wiped out. Using a long for the product keeps
the running multiplication safe while you build the answer, even though the
digit product of a 32-bit int always fits back into an int. Any zero digit
legitimately forces the product to 0, so no special skipping is needed.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Initialising the product to 0, which locks the result at 0.
- Treating a 0-digit result as an error instead of the correct answer.
- Looping while (n > 0) and skipping negative inputs.
Edge Cases to Test
- n = 0 returns 0.
- Any number containing a 0 digit (like 405 or 1020) has product 0.
- Single-digit numbers return that digit.
Interview Follow-Ups
- How would you compute the product of only the non-zero digits?
- How would you guard strictly against overflow for a value stored in a long?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →