Java Program to Check If a Number is Spy number or not
A number is said to be a Spy number if the sum of all the digits is equal to the product of all digits. For performing the task we need to reverse through the number which will take log(N) time.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input : 22 Output: Given number is a SPY number. Explanation: Sum of the number is 4 (2 + 2) Product of the number is as 4 (2 * 2) Input : 1241 Output: Given number is not a SPY number.
- Calculate the sum of digits of the input Number.
- Calculate the product of digits of the input Number.
- If the sum of digits equals the product of digits then the number is Spy number otherwise not.
Below is the implementation of the above approach:
Given number is spy number