SPY Number in JAVA
SPY Number in JAVA
A Number is said to be a SPY Number if the sum and product of all digits are equal.
Example: Number 123 is a Spy number, sum of its digits is 6
Algorithm and Program below :-
ALGORITHM :-
- Get the number to check for Spy Number
- Hold the number in temporary variable
- Store the sum of its digits in a variable
- Store the product of its digits in another variable
- Compare the sum and product digits of the number
- If both are equal print "Spy Number"
- If not print "Not a Spy Number"
PROGRAM below :-
import java.io.*;
import java.util.*;
class Spy
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int n,p,rev,s=0,sum=1;
System.out.println("Enter No.");
n= in.nextInt(); // Input number from user
p=n; // store the entered number in "p" variable
while(n>0)
{
rev=n%10; // it extract last digit of the number
s=s+rev; // store the sum of digits
sum=sum*rev; // store product of digits
n=n/10; // it extract all digit except the last
}
if(sum==s) // comparing with product of digits with sum of digits
{
System.out.println("It is Spy Number : "+p);
}
else
{
System.out.println("It is not Spy Number : "+p);
}
} // end of main method
} // end of class
Follow me on Instagram
All the Best :)
Keep Learning :)
import java.io.*;
import java.util.*;
class Spy
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int n,p,rev,s=0,sum=1;
System.out.println("Enter No.");
n= in.nextInt(); // Input number from user
p=n; // store the entered number in "p" variable
while(n>0)
{
rev=n%10; // it extract last digit of the number
s=s+rev; // store the sum of digits
sum=sum*rev; // store product of digits
n=n/10; // it extract all digit except the last
}
if(sum==s) // comparing with product of digits with sum of digits
{
System.out.println("It is Spy Number : "+p);
}
else
{
System.out.println("It is not Spy Number : "+p);
}
} // end of main method
} // end of class
Follow me on Instagram
All the Best :)
Keep Learning :)
Examples of SPY NUMBER
ReplyDelete