In this tutorial, we will write java programs to check for Spy Numbers. We will look at two different java programs son Spy Number
- Java Program to Check If a Number is Spy number or not.
- Java Program to print all the Spy Number within a given Range.
What is Spy Number?
A number is said to be a spy number if the sum of all its digits is equal to the product of all its digits.
Example:
Number: 123
Sum of its digits: 1+2+3 = 6
Product of its digits: 1*2*3 = 6
Sum and Product are equal, hence, 123 is a Spy number.
Similarly, you may check for other numbers such as 22, 132, 1124, etc.
Spy Number in Java Program
1. Java Program to Check If a Number is Spy number or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.Scanner; public class SpyNumber { public static void main(String args[]) { int num, product = 1, sum = 0, lastdigit; Scanner sc = new Scanner(System.in); //read from user System.out.print("Enter the number: "); num = sc.nextInt(); int number = num; //calculation for sum and product while (num > 0) { lastdigit = num % 10; //calculating sum sum = sum + lastdigit; //calculating product product = product * lastdigit; num = num / 10; } //compares the sum and product and print the result if (sum == product) System.out.println(number + " is a spy number."); else System.out.println(number + " not a spy number."); } } |
Output:
//First Run
Enter the number: 1124
1124 is a spy number.
//Second Run
Enter the number: 166
166 not a spy number.
2. Java Program to print all the Spy Number within a given Range.
We will create a separate Boolean function (isSpyNumber()
) that will return true if the sum of the digits and the product of the digits are equal else will return false and print them accordingly.
The program takes the input for the lower range and upper range value and will pass those numbers between this range to the function as an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import java.util.Scanner; public class SpyNumber { private static boolean isSpyNumber(int number) { int lastdigit = 0; int sum = 0, product = 1; //calculation while (number != 0) { lastdigit = number % 10; //calculating sum sum = sum + lastdigit; //calculating product product = product * lastdigit; number = number / 10; } //comapre and return true value if found equal if (sum == product) return true; return false; } public static void main(String args[]) { int lwRange, upRange; Scanner sc = new Scanner(System.in); //read from user System.out.print("Enter the Lower Range: "); lwRange = sc.nextInt(); System.out.print("Enter the Upper Range: "); upRange = sc.nextInt(); System.out.println("The Spy numbers between " + lwRange + " and " + upRange + " are: "); for (int i = lwRange; i <= upRange; i++) { //calling function at each iteration, that is for each number within the range if (isSpyNumber(i)) System.out.print(i + " "); } } } |
Output:
Enter the Lower Range: 1
Enter the Upper Range: 1000
The Spy numbers between 1 and 1000 are:
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321