Java ProgramsControl FlowCheck Number Spy

Check Number Spy in Java

intermediate·  Control Flow  ·  Loops

Problem

A Spy number is a number where the sum of its digits equals the product of its digits.

Given a number, determine whether it is a Spy number.

Input
1124
Output
1124 is a Spy number: true

Java Program

Java
public class SpyNumberCheck { public static void main(String[] args) { int n = 1124; int original = n; int sum = 0; int product = 1; while (n > 0) { int digit = n % 10; // extract once, update both totals from the same digit sum += digit; product *= digit; n /= 10; } System.out.println(original + " is a Spy number: " + (sum == product)); } }

Output

1124 is a Spy number: true

Core Logic

Extracting each digit once and updating a running sum and a running product together, in the same pass, checks both totals without looping over the digits twice.

How It Works
  1. 1A single while loop peels off each digit of n with % 10 and /= 10.
  2. 2Each digit is added into sum and multiplied into product in the same iteration.
  3. 3product starts at 1, the multiplicative identity, so the first digit multiplies in cleanly.
  4. 4Once every digit has been processed, sum and product are compared directly.
For 1124, the digits 1, 1, 2, 4 sum to 8 and multiply to 8 as well, so they match.
💡

Key Point: Updating both totals inside the same loop, rather than summing in one pass and multiplying in another, means every digit is only extracted once.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: A single pass extracts each digit once, updating both accumulators together, so the digit count is the only thing the cost scales with.

Key Concepts

digit extractionrunning sumrunning product

Approach 2: Java 8

Java
public class SpyNumberCheckStream { public static void main(String[] args) { int n = 1124; int original = n; // A stream can only be consumed once, so sum and product each need their own digit stream int sum = String.valueOf(n).chars().map(c -> c - '0').sum(); int product = String.valueOf(n).chars().map(c -> c - '0').reduce(1, (a, b) -> a * b); System.out.println(original + " is a Spy number: " + (sum == product)); } }

Output

1124 is a Spy number: true

Core Logic

Since a single stream can only be consumed once, summing and multiplying the digits with streams means building two separate digit streams instead of extracting each digit once for both totals.

How It Works
  1. 1String.valueOf(n).chars().map(c -> c - '0') builds a stream of digit values — this expression is used twice, once for each total, since a stream can't be reused after a terminal operation runs.
  2. 2.sum() on the first stream totals the digits.
  3. 3.reduce(1, (a, b) -> a * b) on the second stream multiplies them together, starting from 1 as the multiplicative identity — IntStream has no built-in product().
  4. 4The two independently-computed totals are compared, same as the manual version.
For 1124, one digit stream sums to 8 and a second, separately built digit stream multiplies to 8 as well, so they match.
💡

Key Point: This genuinely costs more than the manual loop — extracting the digits twice, once per stream, instead of updating both totals from a single pass — because a stream offers no way to fork one pass into two independent reductions without a custom collector.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: Each stream still visits all d digits, so building two streams doubles the digit extractions compared to the manual single-pass loop, though both remain linear in d.

Key Concepts

Streamchars()sum()reduce()

Related Programs