Spy Number in Java

In this section, we will learn what is a spy number and also create Java program to check if the given number is Spy or not. The spy number program is frequently asked in Java  coding test.

Spy Number

A positive integer is called a spy number if the sum and product of its digits are equal. In other words, a number whose sum and product of all digits are equal is called a spy number.

Example of Spy Number

Let’s take the number 1124 and check whether the number is a spy or not. First, we will split it into digits (1, 1, 2, 4). After that find the sum and product of all the digits.

Sum=1+1+2+4=8

Product=1*1*2*4=8

We observe that the sum and product of the digits both are equal. Hence, 1124 is a spy number.

Similarly, we can check other numbers also. Some other spy numbers are 22, 123, 132, etc.

Spy Number in Java

Steps to Find Spy Number

  1. Read or initialize a number (n) that you want to check.
  2. Declare two variables sum and product to store sum and product of digits. Initialize sum with 0 and product with 1.
  3. Find the last digit (n%10) of the given number by using the modulo operator.
  4. Add the last digit to the variable sum.
  5. Multiply the last digit with the product variable.
  6. Divide the given number (n) by 10. It removes the last digit.
  7. Repeat steps 3 to 6 until the given number (n) becomes 0.
  8. If the variable sum and product have the same value, then the given number (n) is a spy number, else not a spy number.

Let’s implement the above steps in a Java program.

Spy Number Java Program

SpyNumberExample1.java

  1. import java.util.Scanner;  
  2. public class SpyNumberExample1   
  3. {  
  4. public static void main(String args[])  
  5. {   
  6. int num, product=1, sum=0, lastdigit;  
  7. // create object of scanner  
  8. Scanner sc = new Scanner(System.in);  
  9. System.out.print(“Enter the number to check: ” );  
  10. //reads an integer from the user and stores it in the variable num  
  11. num=sc.nextInt();  
  12. //executes untill the condition becomes false  
  13. while(num>0)  
  14. {  
  15. //finds the last digit of the number      
  16. lastdigit=num%10;  
  17. //adds last digit to the variable sum  
  18. sum=sum+lastdigit;  
  19. //calculates the product  
  20. product=product*lastdigit;  
  21. //removes the last digit from the given number  
  22. num=num/10;  
  23. }  
  24. //compares the sum and product  
  25. if(sum==product)  
  26. //prints if the above condition returns true  
  27. System.out.println(“The given number is a spy number.”);  
  28. else  
  29. //prints if the above condition returns false  
  30. System.out.println(“The given number is not a spy number.”);  
  31. }  
  32. }  

Output 1:

Enter the number to check: 123
The given number is a spy number.

Output 2:

Enter the number to check: 456
The given number is a not spy number.

SpyNumberExample2.java

  1. import java.util.Scanner;  
  2. public class SpyNumberExample2  
  3. {  
  4. //method to check the Spy number  
  5. private static boolean isSpyNumber(int number)   
  6. {  
  7. int lastDigit = 0;  
  8. int sum = 0;  
  9. int product = 1;  
  10. //executes until the condition returns true  
  11. while(number != 0)   
  12. {  
  13. //determines the last digit of the given number  
  14. lastDigit = number % 10;  
  15. //adds the last digit to the variable sum  
  16. sum = sum + lastDigit;  
  17. //multiply last digit with product  
  18. product = product * lastDigit;  
  19. //removes the last digit of the given number  
  20. number = number / 10;  
  21. }  
  22. //compares the variable sum with product and returns the result accordingly  
  23. if(sum == product)  
  24. return true;  
  25. return false;  
  26. }  
  27. //driver code  
  28. public static void main(String args[])   
  29. {  
  30. int lowerRange = 0, upperRange = 0;  
  31. Scanner sc = new Scanner(System.in);  
  32. System.out.print(“Enter the lower range: “);  
  33. //reads lower range  
  34. lowerRange = sc.nextInt();  
  35. System.out.print(“Enter upper range: “);  
  36. //reads the upper range   
  37. upperRange = sc.nextInt();  
  38. System.out.println(“The Spy numbers between “+ lowerRange + ” to “+ upperRange+” are: “);  
  39. for(int i=lowerRange; i<=upperRange; i++)   
  40. {  
  41. //calling user-defined function that checks if the given number is spy or not  
  42. if(isSpyNumber(i))  
  43. //prints all the spy numbers  
  44. System.out.print(i +” “);  
  45. }  
  46. }  
  47. }  

Output:

Enter the lower range: 1
Enter upper range: 10000
The Spy numbers between 1 to 10000 are: 
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421 2114 2141 

Leave a Comment