Question
Write a program to accept a number and check and display whether it is a spy number or not.
(A number is spy if the sum its digits equals the product of the digits.)
Example: consider the number 1124 , sum of the digits = 1 + 1 + 2 + 4 = 8 Product of the digits = 1 x 1 x 2 x 4=8
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program5
{
public static void main(String[] args)
{
int num=0,sum=0,product=1,rem=0,temp=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
temp=num;
while (temp>0)
{
rem = temp % 10;
sum = sum + rem;
product = product * rem;
temp = temp / 10;
}
if (sum == product)
{
System.out.println(num+" is a Spy number");
}
else
{
System.out.println(num+" is Not a spy number");
}
}
}