- 1Java Interview Program Title
- 2Java Interview Program - Check Whether a Number is Armstrong
- 3Java Interview Program - Check Whether a Number is an Ugly Number
- 4Java Interview Program - Check Whether a Number is a Sunny Number in Java
- 5Java Interview Program - Check Whether a Number is a Harshad (Niven) Number
- 6Check whether a number is a Hamming number in Java
- 7Check Whether a Number is a Spy Number in Java
- 8Check whether a Number is a Smith Number in Java
- 9Check Whether a Number is a Disarium Number in Java
- 10Check Whether a Number is a Lychrel Number Candidate in Java
- 11Check Whether a Number is a Tech Number in Java
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:
- Read the input number
- Initialize
sum = 0
andproduct = 1
- Extract digits one by one using
num % 10
andnum / 10
- Add digit to
sum
and multiply withproduct
- Repeat until the number becomes 0
- Compare
sum
andproduct
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.
Next Topic ⮕Check whether a Number is a Smith Number in Java
Comments
Loading comments...