beginnerNumber ProblemsJava

Check Whether a Number Is a Spy Number

Decide whether a number is a spy number by checking if its digit sum equals its digit product.

Quick Answer

A spy number is one where the sum of its digits equals the product of its digits. Walk the digits once with n % 10 and n / 10, keep a running sum and running product, then return true when they are equal. For 1124 the sum is 8 and the product is 8, so it is a spy number.

Problem Statement

A spy number is a positive integer for which the sum of its digits equals the product of its digits. Given an integer n, decide whether it is a spy number and return true or false.

For example, 1124 has digit sum 1 + 1 + 2 + 4 = 8 and digit product 1 * 1 * 2 * 4 = 8. Because the two are equal, 1124 is a spy number.

Input: A single integer n.

Output: A boolean: true if n is a spy number, otherwise false.

Examples

Example 1
Input:  1124
Output: true

Digit sum 1+1+2+4 = 8 equals digit product 1*1*2*4 = 8.

Example 2
Input:  34
Output: false

Digit sum 3+4 = 7 but digit product 3*4 = 12, so they differ.

Constraints

  • n is a non-negative integer that fits in a 32-bit int
  • Only arithmetic on the digits is needed

Think Before You Code

Reveal the questions to ask yourself first
  • How do you read one digit at a time without converting to a String?
  • What starting value does a running product need, and why not 0?
  • Should the sum and product be accumulated in the same loop?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You can peel digits off the right with `n % 10` and shrink the number with `n / 10`.
Hint 2
Keep two accumulators as you loop: `sum += digit` and `product *= digit`.
Hint 3
Initialise `product` to 1 (not 0), then after the loop simply return `sum == product`.

Approach

Reveal the step-by-step approach

Extract the digits once and track both totals as you go.

  1. Take the absolute value of n so a stray sign does not break the loop.
  2. Set sum = 0 and product = 1.
  3. While the number is not 0:
    • digit = n % 10 — the current last digit.
    • sum += digit and product *= digit.
    • n = n / 10 — drop that digit.
  4. Return sum == product.

Initialising product to 1 is essential: starting at 0 would force every product to 0.

Dry Run

Walk through the example step by step

Checking n = 1124:

step | n     | digit | sum          | product
-----+-------+-------+--------------+-------------
1    | 1124  | 4     | 0 + 4 = 4    | 1 * 4 = 4
2    | 112   | 2     | 4 + 2 = 6    | 4 * 2 = 8
3    | 11    | 1     | 6 + 1 = 7    | 8 * 1 = 8
4    | 1     | 1     | 7 + 1 = 8    | 8 * 1 = 8
end  | 0     | —     | 8            | 8

sum (8) == product (8), so 1124 is a spy number.

Solution

Reveal the full Java solution
public class SpyNumber {
    public static boolean isSpy(int n) {
        int num = Math.abs(n);
        int sum = 0;
        int product = 1;
        while (num != 0) {
            int digit = num % 10;
            sum += digit;
            product *= digit;
            num /= 10;
        }
        return sum == product;
    }

    public static void main(String[] args) {
        System.out.println(isSpy(1124)); // true
        System.out.println(isSpy(34));   // false
    }
}

A single pass over the digits is enough because the sum and the product can be built at the same time. The only subtle point is the seed value of product: it must be 1 so that the first multiplication keeps the real first digit, and so a genuine product is formed rather than a forced 0.

Time: O(d) where d is the number of digits (log10 n)Space: O(1)

Common Mistakes

  • Initialising `product` to 0, which makes every product 0.
  • Comparing digit-by-digit instead of comparing the final totals once.

Edge Cases to Test

  • Single-digit numbers like 5: sum 5 equals product 5, so they are spy numbers.
  • A number containing a 0 digit forces the product to 0, e.g. 105 is not a spy number.

Interview Follow-Ups

  • How would you also handle negative inputs by defining the sign behaviour?
  • Can you return the sum and product themselves for debugging without a second loop?

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 →
Chat with us