Skip to content

Commit

Permalink
JAVA Program to check if the given value is spy number or not
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayush Kapur authored and Aayush Kapur committed Sep 24, 2023
1 parent 0ed7ac2 commit 6c526d8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions IsSpyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.");
}
}
}

0 comments on commit 6c526d8

Please sign in to comment.