Java Program To Check Spy Number or Not
Spy Number
A number is said to be a Spy number if the
sum of all the digits is equal to the product of all digits.
Example :
Given Number : 1412
Sum of all the Digits = 1+4+1+2 = 8
Product of all the Digit = 1*4*1*2 = 8
1412 is a Spy Number
Since ,
Sum of all the digits is equal to the product of all digits.
you can use the following algorithm:
- Read the input number.
- Initialize variables sum and product to 0 and 1 respectively.
- While the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Add the last digit to the sum.
- Multiply the last digit to the product.
- Divide the number by 10 to remove the last digit.
- Check if the sum is equal to the product.
- If they are equal, the number is a Spy Number.
Otherwise, it is not. - Print the result.
In this algorithm, the input number is processed by extracting each digit and adding it to the sum variable and multiplying it to the product variable. Then, the algorithm checks if the sum is equal to the product. If they are equal, the number is considered a Spy Number.
Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Here is an example implementation in Java
Source Code
//Java Program To Check Spy Number or Not
import java.util.*;
public class SpyNumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int num ,Rd,sum=0,pro=1;
System.out.print("Enter a Number To Check : ");
num=in.nextInt();
while(num!=0)
{
Rd = num % 10 ;
sum= sum + Rd ;
pro= pro * Rd ;
num= num / 10 ;
}
if(sum==pro)
System.out.print("It is a Spy Number . ");
else
System.out.print("It is Not a Spy Number . ");
}
}
Output
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is a Spy Number
Sample Input :
Enter a Number To Check : 1421
Sample Output:
It is a Spy Number
Sample Input :
Enter a Number To Check : 125
Sample Output:
It is Nota Spy Number
Looping Related Programs
- Java Program to Check Prime Number or Not
- Java Program to Check Twin Prime Number or Not
- Java Program to Check Perfect Number or Not
- Java Program to Check Spy Number or Not
- Java Program to Check Neon Number or Not
- Java Program to Check Duck Number or Not
- Java Program to Check Niven or Harshad Number or Not
- Java Program to Check Palindrome Number or Not
- Java Program to Check Armstrong Number or Not
- Java Program to Check Strong (Krishnamurthy) Number or Not
- Java Program to Check Automorphic Number or Not
- Java Program to Check Happy Number Or Not