-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAVA Program to check if the given value is spy number or not
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |