Spy Number Program in Java with Examples

In this tutorial, I will be sharing what is a Spy number, examples of Spy number, algorithm, and java program to check whether a given number is a Spy number or not. 

Read Also: Harshad Number in Java

What is Spy Number

Spy numbers are those numbers whose sum of all digits is equal to the product of all digits.

Spy number in java
Examples:

Number to check: 123  
Sum of digits: 1+2+3 = 6 
Product of digits: 1*2*3 = 6  
Is Product of digits equals Sum of digits : Yes // 123 is a Spy number
 
Number to check: 1124  
Sum of digits: 1+1+2+4 = 8 
Product of digits: 1*1*2*4 = 8  
Is Product of digits equals Sum of digits : Yes // 1124 is a Spy number

Below are examples of numbers which are NOT Spy numbers:

Number to check: 180  
Sum of digits: 1+8+0 = 9 
Product of digits: 1*8*0 = 0  
Is Product of digits equals Sum of digits : No // 180 is NOT a Spy number
 

Number to check: 234  
Sum of digits: 2+3+4 = 9 
Product of digits: 2*3*4 = 24  
Is Product of digits equals Sum of digits : No // 234 is NOT a Spy number


Read Also: Perfect Number in Java

Java Program for Spy Number

import java.util.*;

public class JavaHungry {
    public static void main(String args[]) {
        System.out.println("Enter any number : ");
        Scanner scan = new Scanner(System.in);
        Integer inputNumber = scan.nextInt();
        checkSpyNumber(inputNumber);
    }
    
    public static void checkSpyNumber(int inputNumber)
    {
        int number = inputNumber;
        int rightMostDigit, sumOfDigits, productOfDigits;
        sumOfDigits = 0;
        productOfDigits = 1;
        while(number != 0)
        {
            rightMostDigit = number % 10;
            sumOfDigits = sumOfDigits + rightMostDigit;
            productOfDigits = productOfDigits * rightMostDigit;
            number = number / 10;
        }
        
        if (sumOfDigits == productOfDigits)
            System.out.println(inputNumber + " is a Spy number");
        else 
            System.out.println(inputNumber + " is NOT a Spy number");            
    }
}


Output:
Enter any number :
234 is NOT a Spy Number

Algorithm for Spy Number

1. Calculate the sum of digits of the inputNumber.
2. Calculate the product of digits of the inputNumber.
3. If the sum of digits equals the product of digits then the number is Spy number otherwise not.

That's all for today, please mention in comments in case you have any questions related to the Spy number program in java with examples.

You may also like:

Special number program in Java (solution)
Pronic number program in Java (answer)
Bouncy number program in Java (answer)
Disarium number program in Java (answer)
Duck number program in Java (answer)  
Perfect number program in Java (solution)
Neon number program in Java (solution)
Magic number program in Java (solution)
Happy number program in Java (answer)

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry