forked from sujana-kamasany/javacodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsSpyNumber.java
33 lines (28 loc) · 1.08 KB
/
IsSpyNumber.java
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
31
32
33
import java.math.BigInteger;
import java.util.Scanner;
public class IsSpyNumber {
// Function to check if a number is a spy number
public static boolean isSpyNumber(BigInteger num) {
BigInteger sumOfDigits = BigInteger.ZERO;
BigInteger productOfDigits = BigInteger.ONE;
BigInteger ten = BigInteger.TEN;
while (num.compareTo(BigInteger.ZERO) > 0) {
BigInteger[] divRem = num.divideAndRemainder(ten);
BigInteger digit = divRem[1];
sumOfDigits = sumOfDigits.add(digit);
productOfDigits = productOfDigits.multiply(digit);
num = divRem[0];
}
return sumOfDigits.equals(productOfDigits);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
BigInteger number = scanner.nextBigInteger();
if (isSpyNumber(number)) {
System.out.println(number + " is a spy number.");
} else {
System.out.println(number + " is not a spy number.");
}
}
}