How To Check Whether a Number is a Spy Number or Not in Java?


A number is said to be a Spy number, if the sum of the digits of the given number is equal to the product of the digits of the given number.

For more clarification, we have to first calculate the sum of the digits of the given number. After that we have to calculate the product of the digits of the given number. Then we have to compare both the sum value and product value, if they are the same then the given number is a spy number otherwise it is not a spy number.

In this article, we will see how to check if a number is a Spy number by using Java programming language.

To show you some instances

Instance-1

Input number is 123.

Let’s check it by using the logic of Spy number.

The sum of the digits of 123 = 1 + 2 + 3 = 6
The product of the digits of 123 = 1 * 2 * 3 = 6

As we notice here both the sum value and product value are the same.

Hence, 123 is a Spy number.

Instance-2

Input number is 22.

Let’s check it by using the logic of Spy number.

The sum of the digits of 22 = 2 + 2 = 4
The product of the digits of 22 = 2 * 2 = 4

As we notice here both the sum value and product value are the same.

Hence, 22 is a Spy number.

Instance-3

Input number is 234.

Let’s check it by using the logic of Spy number.

The sum of the digits of 234 = 2 + 3 + 4 = 9
The product of the digits of 234 = 2 * 3 * 4 = 24

As we notice here both the sum value and product value are not same.

Hence, 234 is not a Spy number.

Algorithm

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − Then take a loop and using the loop extract the digits and calculate sum value and product value.

  • Step 3 − Finally, when the loop ends you have the total sum value and product value.

  • Step 4 − If both the sum value and the product value are equal to each other then we can print the result as the given number is a spy number otherwise the number is not a spy number.

Multiple Approaches

We have provided the solution in 2 different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach one integer value will be initialized in the program and then by using the algorithm we can check whether a number is a spy number or not.

Example

public class Main { public static void main(String args[]) { //declare the variables int inputNumber,org, productValue=1, sumValue=0, digit; //initialize the value to the variable inputNumber=123; //store that value to another variable for result print org=inputNumber; //continue the loop till the number is not zero while(inputNumber>0) { //extracting the unit place's digit digit=inputNumber%10; //continuously add that digit to sum variable sumValue=sumValue+digit; //continuously multiply the digit to the product variable productValue=productValue*digit; //removes that digit from the inputNumber inputNumber=inputNumber/10; } //check the condition //whether the sum value is equal to product value or not if(sumValue==productValue) //prints if the condition returns true System.out.println(org + " is a spy number."); else //prints if the condition returns false System.out.println(org + " is not a spy number."); } }

Output

123 is a spy number.

Approach-2: By Using User Defined Method

In this approach, an integer value will be initialized in the program and then we will call a user defined method by passing this input number as parameter.

Inside the method we will check whether a number is a disarium number or not by using the algorithm.

Example

public class Main { //main method public static void main(String args[]) { //declare an int variable and initialize number as value int inp = 123; //call the method and return the value to the if condition if(checkSpy(inp)) System.out.println(inp + " is a spy number."); else System.out.println(inp + " is not a spy number."); } //user defined method to check spy number public static boolean checkSpy(int inputNumber) { //declare the variables int productValue=1, sumValue=0, digit; //continue the loop till the number is not zero while(inputNumber>0) { //extracting the unit place's digit digit=inputNumber%10; //continuously add that digit to sum variable sumValue=sumValue+digit; //continuously multiply the digit to the product variable productValue=productValue*digit; //removes that digit from the inputNumber inputNumber=inputNumber/10; } //check the condition //whether the sum value is equal to product value or not if(sumValue==productValue) return true; else return false; } }

Output

123 is a spy number.

In this article, we explored how to check a number whether it is a spy number or not in Java by using three different approaches.

Updated on: 17-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements