Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span> args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//declare the variables</span>
<span class="token keyword">int</span> inputNumber<span class="token punctuation">,</span>org<span class="token punctuation">,</span> productValue<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> sumValue<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> digit<span class="token punctuation">;</span>
<span class="token comment">//initialize the value to the variable</span>
inputNumber<span class="token operator">=</span><span class="token number">123</span><span class="token punctuation">;</span>
<span class="token comment">//store that value to another variable for result print</span>
org<span class="token operator">=</span>inputNumber<span class="token punctuation">;</span>
<span class="token comment">//continue the loop till the number is not zero</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>inputNumber<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//extracting the unit place's digit</span>
digit<span class="token operator">=</span>inputNumber<span class="token operator">%</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token comment">//continuously add that digit to sum variable</span>
sumValue<span class="token operator">=</span>sumValue<span class="token operator">+</span>digit<span class="token punctuation">;</span>
<span class="token comment">//continuously multiply the digit to the product variable</span>
productValue<span class="token operator">=</span>productValue<span class="token operator">*</span>digit<span class="token punctuation">;</span>
<span class="token comment">//removes that digit from the inputNumber</span>
inputNumber<span class="token operator">=</span>inputNumber<span class="token operator">/</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//check the condition</span>
<span class="token comment">//whether the sum value is equal to product value or not</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>sumValue<span class="token operator">==</span>productValue<span class="token punctuation">)</span>
<span class="token comment">//prints if the condition returns true</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>org <span class="token operator">+</span> <span class="token string">" is a spy number."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token comment">//prints if the condition returns false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>org <span class="token operator">+</span> <span class="token string">" is not a spy number."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span>
<span class="token comment">//main method</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span> args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//declare an int variable and initialize number as value</span>
<span class="token keyword">int</span> inp <span class="token operator">=</span> <span class="token number">123</span><span class="token punctuation">;</span>
<span class="token comment">//call the method and return the value to the if condition</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">checkSpy</span><span class="token punctuation">(</span>inp<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>inp <span class="token operator">+</span> <span class="token string">" is a spy number."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>inp <span class="token operator">+</span> <span class="token string">" is not a spy number."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//user defined method to check spy number</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">boolean</span> <span class="token function">checkSpy</span><span class="token punctuation">(</span><span class="token keyword">int</span> inputNumber<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//declare the variables</span>
<span class="token keyword">int</span> productValue<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> sumValue<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> digit<span class="token punctuation">;</span>
<span class="token comment">//continue the loop till the number is not zero</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>inputNumber<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//extracting the unit place's digit</span>
digit<span class="token operator">=</span>inputNumber<span class="token operator">%</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token comment">//continuously add that digit to sum variable</span>
sumValue<span class="token operator">=</span>sumValue<span class="token operator">+</span>digit<span class="token punctuation">;</span>
<span class="token comment">//continuously multiply the digit to the product variable</span>
productValue<span class="token operator">=</span>productValue<span class="token operator">*</span>digit<span class="token punctuation">;</span>
<span class="token comment">//removes that digit from the inputNumber</span>
inputNumber<span class="token operator">=</span>inputNumber<span class="token operator">/</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//check the condition</span>
<span class="token comment">//whether the sum value is equal to product value or not</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>sumValue<span class="token operator">==</span>productValue<span class="token punctuation">)</span>
<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
