WRITE A PROGRAM TO CHECK WHETHER IT IS SPY NUMBER OR NOT?(A NUMBER IS SPY NUMBER IF SUM IS EAUAL TO 1 AFTER REDUCING TO SINGLE DIGIT. Example:91 sum=10 now sum=1+0=1 It is spy number)
Answers
Explanation:
A number is a Spy number, if sum and product of all digits are equal.
Example:
Number 123 is a Spy number, sum of its digits is 6 (1+2+3 =6) and product of its digits is 6 (1x2x3 = 6), sum and product are same, thus, 123 is a spy number.
import java.util.Scanner;
public class SpyNumber
{
public static void main(String[] args)
{
int n,product=1,sum=0;
int ld;
// create object of scanner.
Scanner sc = new Scanner(System.in);
// you have to enter number here.
System.out.print("Enter the number :" );
// read entered number and store it in "n".
n=sc.nextInt();
// calculate sum and product of the number here.
while(n>0)
{
ld=n%10;
sum=sum+ld;
product=product*ld;
n=n/10;
}
// compare the sum and product.
if(sum==product)
System.out.println("Given number is spy number");
else
System.out.println("Given number is not spy number");
}
}
PLEASE MARK MY ANSWER BRAINLIEST.
Answer:
import java.util.Scanner;
public class Spy
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
int n,r,sum=0,pr=1;
System.out.println("Enter a number");
n=in.nextInt();
while (n>0)
{
r=n%10;
sum=sum+r;
pr=pr*r;
n=n/10;
}
System.out.println("Sum of the Digits="+sum);
System.out.println("Product of the Digits="+pr);
if(sum==pr)
{
System.out.println("The Number is a Spy number");
}
else
{
System.out.println("The Number is not a Spy number");
}
}
}
Hope it helps...