A number is called SPY if the sum of the digits equals the product of the digits.
Example:
123 is a SPY number
1+2+3 = 6
1*2*3 = 6
Let's start the code of determining if a number is SPY or not.
Below is the code for determining if a number is SUPERSPY.
import java.util.Scanner;
class SPY
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum = 0,temp = n, mul = 1;
while(temp!=0)
{
sum+=temp%10;
mul*=temp%10;
temp/=10;
}
if(sum == mul)
{
System.out.println("It is a SPY number");
}
else
{
System.out.println("It is not a SPY number");
}
}
}
Demonstration:
1. First, Scanner class is imported.
2. A class namely SPY have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type
7. Then, three different variables namely
a) sum - This variable will store the sum of digits. First, it is initialized with 0.
b) temp - This variable will hold the number given by user on temporary basis. Actually, entire operation will be done with temp variable. temp = n means the value stored in n namely variable is getting assigned to temp. It means that throughout the entire operation, original value stored in n namely variable will remain safe.
c) mul - This variable will store the sum of digits. First, it is initialized with 1.
Now, let us see the working:
while(temp!=0)
This loop will continue its iteration until value of temp doesn't become equals to 0. Say for example, user gave 123. Here, first 3 will be extracted, then 2 and at last, 1 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.
sum+ = temp%10;
Here, we need to understand few things.
First, individual digits from given number is getting extracted using temp%10.
As in case of 123, the first extracted number using temp%10 will be 3.
Then, this number will get added to the variable sum.
Similarly, second number using temp%10 will be 2 and it will also be added to sum.
At last, third number using temp%10 will be 1 and it will also be added to sum.
Now, sum is containing 3+2+1 = 6
Next operation is mul* = temp%10;
Here, again we need to understand few things.
First, individual digits from given number is getting extracted using temp%10.
As in case of 123, the first extracted number using temp%10 will be 3.
Then, this number will get multiply by the variable mul.
Similarly, second number using temp%10 will be 2 and it will also get multiply by the variable mul.
At last, third number using temp%10 will be 1 and it will also get multiply by the variable mul.
Now, mul is containing 3*2*1 = 6
temp/=10;
In previous operations, we have seen that the first extracted number using temp%10 was 3. Immediately, after these operations, temp/=10; will be executed which means 123 is now going to be divided by 10 resulting to 12.3. This operation will permanently delete this 3 from temp variable. so that in next iteration, 3 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from temp variable.
Here is the final outcome:
sum is now containing sum of digits i.e. 1+2+3 = 6
mul is now containing product of digits i..e 1*2*3 = 6
Finally, with the conditional statement using if-else, it is determined if sum is equal to mul or not as it can be seen in the program. If sum is equal to mul, then it is displaying that the number is a SPY number. Otherwise, it is displaying that the number is not a SPY number.
Now, write this program and test it with various numbers and increase your knowledge.
Happy Coding.......