-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpyNumberCheck.java
More file actions
30 lines (27 loc) · 908 Bytes
/
SpyNumberCheck.java
File metadata and controls
30 lines (27 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 40. Write a program to check if a given number is a spy number.
import java.util.Scanner;
public class SpyNumberCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isSpy = isSpyNumber(num);
if (isSpy) {
System.out.println(num + " is a spy number.");
} else {
System.out.println(num + " is not a spy number.");
}
scanner.close();
}
public static boolean isSpyNumber(int num) {
int sumOfDigits = 0;
int productOfDigits = 1;
while (num > 0) {
int digit = num % 10;
sumOfDigits += digit;
productOfDigits *= digit;
num /= 10;
}
return sumOfDigits == productOfDigits;
}
}