In this tutorial, we will write a java program to check whether a given number is Spy number or not. We will also write a program to find all the Spy numbers in a given range.
What is a Spy Number?
A number is called spy number if the sum of its digits is equal to the product of its digits. For example 123 is a Spy number because:
number = 123 sum of its digits = 1+2+3 = 6 product of its digits = 1*2*3 = 6 sum of digits = product of digits Hence 123 is a Spy number
Similarly 1124 is a Spy number:
number = 1124 sum of its digits = 1+1+2+4 = 8 product of its digits = 1*1*2*4 = 8 sum of digits = product of digits Hence 1124 is a Spy number
Lets take a random number 256 and check for Spy number:
number = 256 sum of its digits = 2+5+6 = 13 product of its digits = 2*5*6 = 60 sum of digits != product of digits Hence 256 is NOT a Spy number
Spy Number Program in Java
Steps to check for Spy number:
- Read the number entered by user using Scanner class.
- Declare and initialize two variable
sumProductDigits
andsumNumberDigits
. - Find the last digit (number%10) of the input number by using the modulo operator.
- Add the last digit found in step 3 to the variable
sumProductDigits
and multiply the last digit with thesumNumberDigits
. - Divide the number by 10 to remove the last digit so that next time the second last digit gets used in the addition and multiplication.
- Repeat steps from step 3 to step 6 until the given number number becomes 0.
- If the value of
sumProductDigits
andsumProductDigits
are equal, then display number is a spy number, else display number is not a spy number.
import java.util.Scanner; public class JavaExample { public static void main(String args[]) { int num, sumProductDigits=1, sumNumberDigits=0, temp; Scanner scanner = new Scanner(System.in); System.out.print("Enter any number: " ); num=scanner.nextInt(); scanner.close(); //copying the value of num in another variable //to perform operations on it. int number = num; while(number>0) { temp=number%10; sumNumberDigits=sumNumberDigits+temp; sumProductDigits=sumProductDigits*temp; number=number/10; } if(sumNumberDigits==sumProductDigits) System.out.println(num+" is a spy number."); else System.out.println(num+" is not a spy number."); } }
Output:
Enter any number: 123 123 is a spy number.
Output 2:
Enter any number: 235 235 is not a spy number.
Java Program to find all the Spy numbers in a given range
Here we are finding all the spy numbers in a given range. We have created a user-defined method checkSpyNumber()
, which has the same logic that we have used in above program for checking if a number is Spy number.
The user is asked to enter start and end range, a loop runs from start range till end range and check for each number in between for spy number using checkSpyNumber()
method. If the method returns true program displays the number else it skips the number. That’s how all the Spy number between start range and end range gets displayed.
import java.util.Scanner; public class JavaExample { private static boolean checkSpyNumber(int num) { int lastDigit; int sumNumberDigits = 0, sumProductDigits = 1; while(num != 0) { lastDigit = num % 10; sumNumberDigits = sumNumberDigits + lastDigit; sumProductDigits = sumProductDigits * lastDigit; num = num / 10; } if(sumNumberDigits == sumProductDigits) return true; return false; } public static void main(String args[]) { int startRange, endRange; Scanner scanner = new Scanner(System.in); System.out.print("Enter the starting range: "); startRange = scanner.nextInt(); System.out.print("Enter the ending range: "); endRange = scanner.nextInt(); scanner.close(); System.out.println("All Spy numbers between "+ startRange + " and "+ endRange+" are: "); for(int i=startRange; i<=endRange; i++) { if(checkSpyNumber(i)) System.out.print(i +" "); } } }
Output:
Enter the starting range: 1 Enter the ending range: 100 All Spy numbers between 1 and 100 are: 1 2 3 4 5 6 7 8 9 22
Leave a Reply