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
). 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");
}
}
}
Enter a number: 1124
1124 is a spy number
Enter a number: 1024
1024 is not a spy number
sum
and product
of its digits using a while
loop. 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.