Spy Number in Java

In this section, we will explore the concept of spy numbers and develop Java programs to determine if a given number is a spy number or not. The spy number program is often encountered in Java coding assessments.

What is Spy Number?

A spy number refers to a positive integer where the sum of its digits is equal to the product of its digits. Put simply, a number is classified as a spy number if the sum and product of all its digits are the same.

Example of Spy Number

Let’s consider the number 1124 and determine if it is a spy number. We start by separating the number into its digits: 1, 1, 2, 4. Next, we calculate the sum and product of these digits.

Sum = 1 + 1 + 2 + 4 = 8

Product = 1 * 1 * 2 * 4 = 8

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

Similarly, we can apply this process to other numbers as well. Some other examples of spy numbers include 22, 123, and 132.

Steps to Find Spy Number

  • Read or initialize a number (n) that you want to check.
  • Declare two variables, sum, and product, to store the sum and product of digits.
  • Initialize sum with 0 and product with 1. Iteratively perform the following steps until the given number (n) becomes 0:
  • Find the last digit of the given number by using the modulo operator: last digit = n % 10.
  • Add the last digit to the variable sum: sum += last digit.
  • Multiply the last digit with the product variable: product *= last digit.
  • Divide the given number (n) by 10 to remove the last digit: n /= 10. If the sum and product of the variables have the same value, then the given number (n) is a spy number; otherwise, it is not a spy number.

Spy Number Java Program

SpyNumberExample.java

import java.util.Scanner;  
public class SpyNumberExample   
{  
public static void main(String args[])  
{   
int num, product=1, sum=0, lastdigit;  
// create object of scanner  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter the number to check: " );  
//reads an integer from the user and stores it in the variable num  
num=sc.nextInt();  
//executes untill the condition becomes false  
while(num>0)  
{  
//finds the last digit of the number      
lastdigit=num%10;  
//adds last digit to the variable sum  
sum=sum+lastdigit;  
//calculates the product  
product=product*lastdigit;  
//removes the last digit from the given number  
num=num/10;  
}  
//compares the sum and product  
if(sum==product)  
//prints if the above condition returns true  
System.out.println("The given number is a spy number.");  
else  
//prints if the above condition returns false  
System.out.println("The given number is not a spy number.");  
}  
}

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.

SpyNumberExample1.java

import java.util.Scanner;  
public class SpyNumberExample1  
{  
//method to check the Spy number  
private static boolean isSpyNumber(int number)   
{  
int lastDigit = 0;  
int sum = 0;  
int product = 1;  
//executes until the condition returns true  
while(number != 0)   
{  
//determines the last digit of the given number  
lastDigit = number % 10;  
//adds the last digit to the variable sum  
sum = sum + lastDigit;  
//multiply last digit with product  
product = product * lastDigit;  
//removes the last digit of the given number  
number = number / 10;  
}  
//compares the variable sum with product and returns the result accordingly  
if(sum == product)  
return true;  
return false;  
}  
//driver code  
public static void main(String args[])   
{  
int lowerRange = 0, upperRange = 0;  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter the lower range: ");  
//reads lower range  
lowerRange = sc.nextInt();  
System.out.print("Enter upper range: ");  
//reads the upper range   
upperRange = sc.nextInt();  
System.out.println("The Spy numbers between "+ lowerRange + " to "+ upperRange+" are: ");  
for(int i=lowerRange; i<=upperRange; i++)   
{  
//calling user-defined function that checks if the given number is spy or not  
if(isSpyNumber(i))  
//prints all the spy numbers  
System.out.print(i +" ");  
}  
}  
}

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 2411 4112 4121 4211

We hope you now have a better understanding of Spy Number in Java. To expand your knowledge further, be sure to follow tutorials.freshersnow.com.