Computer Science, asked by ShivamSharma111, 1 year ago

write to input a number and check whether it is a spy number or not . (Using java language)

Answers

Answered by Anonymous
65
Hey Friend,

Spy number is a number where the product of digits is equal to the sum of digits. 
Example - 231
Sum of digits is 2+3+1 = 6
Product of digits is 2*3*1 = 6

Hence, Sum of Digits is equal to the product of digits of the number.

Here is your program...

import java.io.*
class Spy
{
public static void main () throws IOException
{
System.out.println ("Enter a number");

BufferedReader br = new BufferedReader (new InputStreamReader (System.in) );

int n = Integer.parseInt (br.readLine()); // to read the number
int n1 = n; // to store the value of n
int sum = 0; // to find the sum of digits 
int prod = 1; // to find the product of digits
int rem = 0; // to find the remainder to get individual digits

while (n>0)
{
rem = n%10;
sum = sum + rem;
prod = prod * rem;
n = n/10;
}

if (sum==prod)
System.out.println ("Yes! It is a Spy Number");
else
System.out.println ("Sorry! It is not a Spy Number");

}
}

Hope it helps!

Dhruv00: Lol finally u got it !!!!
ShivamSharma111: please use scanner class
AnviGottlieb: Nice :D
Answered by Dhruv00
32
import java.util.*;

class hawwwwww
{
static void main (String yo [])
{
Scanner ehhhh= new Sacnner(System.in);

System.out.println("Enter a number to be checked");

int sure = ehhhh.nextInt();

int s = 0; int p=1;

while (sure>0)
{
int digit = sure%10; //extracting digits

s= s+digit; // storing sum of digits in s

p=p*digit;// storing product of digits in p

sure = sure/10;
}

if (s==p) // main condition of spy number
System.out.print("Bingo your number is a spy");

else
System.out.print("Ummmm sorry your number isn't spy");

}
}




Dhruv00: Hope this helps you!!
Anonymous: Yo! Nice class name n object names! ;P
Dhruv00: ;P
AnviGottlieb: Bhai kiska hai B)
Similar questions