Check Whether a Number is a Spy Number in Java

Topic

A Spy Number is a number where the sum of its digits is equal to the product of its digits. For example, 1124 is a spy number because 1+1+2+4 = 8 and 1×1×2×4 = 8.

To check this in Java, we need to extract each digit of the number, compute the sum and product of the digits, and then compare the two values. This involves using loops, arithmetic operations, and conditionals — all of which are fundamental for Java beginners.

Examples

  • Input: 1124
    Explanation: Sum = 1+1+2+4 = 8, Product = 1×1×2×4 = 8 → Spy Number ✅
  • Input: 123
    Explanation: Sum = 1+2+3 = 6, Product = 1×2×3 = 6 → Spy Number ✅
  • Input: 134
    Explanation: Sum = 1+3+4 = 8, Product = 1×3×4 = 12 → Not a Spy Number ❌
  • Edge Case Input: 0
    Explanation: Sum = 0, Product = 1 (as product starts at 1) → Not a Spy Number ❌

Interviewer Expectations

The interviewer wants to assess:

  • Your understanding of number manipulation using loops
  • Your ability to use basic arithmetic operators
  • Your skill in breaking a number into digits
  • Your attention to edge cases (e.g., single digit numbers, zero)

Approach

Follow these steps:

  1. Read the input number
  2. Initialize sum = 0 and product = 1
  3. Extract digits one by one using num % 10 and num / 10
  4. Add digit to sum and multiply with product
  5. Repeat until the number becomes 0
  6. Compare sum and product

Dry Run: Input = 1124

  • Digits: 4, 2, 1, 1
  • Sum = 1 + 1 + 2 + 4 = 8
  • Product = 1 × 1 × 2 × 4 = 8
  • Result: Spy Number ✅

Java Program

public class SpyNumberCheck {
    public static void main(String[] args) {
        int num = 1124;
        int sum = 0;
        int product = 1;
        int temp = num;

        while (temp > 0) {
            int digit = temp % 10;
            sum += digit;
            product *= digit;
            temp /= 10;
        }

        System.out.println("Sum of digits: " + sum);
        System.out.println("Product of digits: " + product);

        if (sum == product) {
            System.out.println(num + " is a Spy Number.");
        } else {
            System.out.println(num + " is NOT a Spy Number.");
        }
    }
}
Sum of digits: 8
Product of digits: 8
1124 is a Spy Number.

Possible Followup Questions with Answers

Q1: What if the number is a single digit?

Any single-digit number will have sum = digit and product = digit, so it will always be a spy number.

Q2: Can we write a method to check any number dynamically?

public static boolean isSpyNumber(int num) {
    int sum = 0, product = 1;
    while (num > 0) {
        int digit = num % 10;
        sum += digit;
        product *= digit;
        num /= 10;
    }
    return sum == product;
}

Q3: Can we use String methods instead of loops?

Yes, you can convert the number to a String and loop through each character using charAt() and Character.getNumericValue(), though it may be less efficient for very large numbers.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...