➤ Even Number in Java
➤ Odd Number in Java
➤ Prime Number in Java
➤ Twin Prime Number
➤ Magic Number in Java
➤ Neon Number in Java
➤ Tech Number in Java
➤ Harshad Number
➤ Armstrong Number
➤ Palindrome Number
➤ Perfect Number in Java
➤ Pronic Number in Java
➤ Spy Number in Java
➤ Kaprekar Number
➤ Automorphic Number
➤ Krishnamurthy Number
➤ Sunny Number in Java
➤ Buzz Number in Java
➤ Evil Number in Java
➤ Duck Number in Java
➤ Nelson Number in Java
➤ Strong Number in Java
➤ Java Special Number
➤ Disarium Number
Java Number Program Using String
➤ Unique Number in Java
➤ Fascinating Number
➤ ISBN Number in Java
In this post, we will develop a Java program to check whether the given number is a spy number or not? We will also develop a Java program to find all the spy number exist in between two given numbers.
A number whose sum of the digits of is equal to the product of its digits is called Spy number.
Example:- 132
The sum of digits = 1 + 3 + 2 = 6
The product of the digits = 1 * 3 * 2 = 6
Hence, 132 is a spy number.
Similarly, 1124 is also a spy number.
The sum of digits = 1 + 1 + 2 + 4 = 8
The product of the digits = 1 * 1 * 2 * 4 = 8
Procedure to develop method to check the given number is spy number or not,
1) Take a number
2) Declare sum and product variables. Initialize the sum variable with 0 and the product variable with 1.
3) Find the last digit of the number
4) Add last digit to the sum variable
5) Multiply last digit with the product variable
6) Remove the last digit of the number
7) Repeat 3 to 6 steps until the number becomes 0
8) If the value of sum and product variable is equal then the given number is spy number else it is not a spy number.
import java.util.Scanner;
public class SpyNumber {
// method to check the Spy number
private static boolean isSpy(int number) {
// declare variables
int lastDigit = 0;
int sum = 0;
int product = 1;
// iterate all digits of number
while(number != 0) {
// find last digit
lastDigit = number % 10;
// add last digit to sum
sum = sum + lastDigit;
// multiply last digit with product
product = product * lastDigit;
// remove last digit
number = number / 10;
}
// compare sum and product
if(sum == product)
return true;
return false;
}
// main method
public static void main(String[] args) {
// declare variables
int number = 0;
boolean result = false;
// create Scanner class object
// to take input
Scanner scan = new Scanner(System.in);
// take input from end-user
System.out.print("Enter an integer number::");
number = scan.nextInt();
// check number is Spy number or not
result = isSpy(number);
// display result
if(result)
System.out.println(number +
" is a Spy number.");
else
System.out.println(number +
" is not a Spy number.");
// close Scanner class object
scan.close();
}
}
The output of the different test-cases:-
Enter an integer number::132
132 is a Spy number.
Enter an integer number::133
133 is not a Spy number.
Also see:- Special number, Magic number, Armstrong number, Perfect number, Evil Number, Spy Number, Sunny number in Java
Java program to find all spy number in a given range
We can also find all spy number which exists between two given numbers. For this, we need to use a loop.
import java.util.Scanner;
public class SpyNumberInRange {
// method to check the Spy number
private static boolean isSpy(int number) {
// declare variables
int lastDigit = 0;
int sum = 0;
int product = 1;
// iterate all digits of number
while(number != 0) {
// find last digit
lastDigit = number % 10;
// add last digit to sum
sum = sum + lastDigit;
// multiply last digit with product
product = product * lastDigit;
// remove last digit
number = number / 10;
}
// compare sum and product
if(sum == product)
return true;
return false;
}
// main method
public static void main(String[] args) {
// declare variables
int minRange = 0, maxRange = 0;
//create Scanner class object to take input
Scanner scan = new Scanner(System.in);
System.out.print("Enter minimum value of range:");
minRange = scan.nextInt();
System.out.print("Enter maximum value of range:");
maxRange = scan.nextInt();
// loop
System.out.println("The Spy number from "+
minRange + " to "+ maxRange+" are: ");
for(int i=minRange; i<=maxRange; i++) {
// check number
if(isSpy(i))
System.out.print(i +" ");
}
// close Scanner class object
scan.close();
}
}
The output of the different test-cases:-
Enter minimum value of range: 1
Enter maximum
value of range: 1000
The Spy number from 1 to 1000 are:1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321
Enter minimum value of range: 1000
Enter maximum
value of range: 2000
The Spy number from 1000 to 2000 are:1124 1142 1214 1241 1412 1421
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!