Google News
logo
Java program to Spy Number
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 the sum of its digits (1+1+2+4 = 8) is equal to the product of its digits (112*4 = 8).

Here's a Java program to check if a given number is a spy number or not :
Program :
import java.util.Scanner;

public class SpyNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        int sum = 0;
        int product = 1;
        int temp = number;
        while (temp > 0) {
            int digit = temp % 10;
            sum += digit;
            product *= digit;
            temp /= 10;
        }
        if (sum == product) {
            System.out.println(number + " is a spy number");
        } else {
            System.out.println(number + " is not a spy number");
        }
    }
}
Output :
Enter a number: 1124
1124 is a spy number
Enter a number: 1024
1024 is not a spy number
In this program, we take an input number from the user and then compute the sum and product of its digits using a while loop.

If the sum is equal to the product, we print that the number is a spy number, otherwise we print that it is not a spy number.