Mystery Number in Java

In this section, we will learn what is a mystery number and also create Java program to check if the given number is a mystery number or not. The mystery number program frequently asked in Java coding tests and academics.

Mystery Number

A number N is said to be a mystery number if the number can be expressed by the sum of two numbers. Note that these two numbers must be reverse of each other. It lies between 22 to 198, i.e. 22<=N<=198.

It is called mystery numbers because these are the multiples of 11, and the sum of place values of unit (1) and tens (10) place is 11 in decimal numeration.

Sometimes, we get it in the form of a question, as follows:

A number is split into two ‘2-digit’ numbers in such a way that its unit and tens digits are interchanged. Find the mystery number?

Let’s understand it mathematically.

Suppose, y is a unit digit and X is a tens digit. Then, the original number will be 10X+Y. After interchanging the place of the unit and tens digit, we get 10Y+X. Therefore, the mystery number will be:

Mystery number= 10X+Y+10Y+X

Solving the above equation, we get:

11X+11Y

Take 11 as common. Hence, 11(X+Y).

Here, X and Y can be replaced by any whole number such that and (as a condition is given in two-digit numbers).

Let’ see some examples based on the above equation.

Mystery Number Example

Putting the values of X and Y in the above equation.

11 ( 9+3) = 132 = 93+39

11 (6 + 8) = 154 = 68 + 86

11 (7+9 ) = 176 = 79 + 97

11 (5 +5)= 110 = 55 + 55

11 (2+9)= 121 =29+92

11 (3+8)= 121 = 83+38

11 (5+6)= 121 =56+65

11 (1+1)= 22 =11+11

Similarly, we can check other numbers also. We can get problems based on mystery numbers. For example:

Example 1: Find the mystery number between 81 and 95 that contains 0 at the unit place.

Let, N is a mystery number. We have to find 81

Steps to Find Mystery Number

  1. Read or initialize a number N.
  2. Convert the given number N into String.
  3. Find the reverse of the String.
  4. Again, convert the string (reverse) into an integer.
  5. Sum up the pair of numbers.
  6. Compare the sum with the given number.
    • If the sum and the given number are equal, the number is a mystery
    • Else, not a mystery number.

Let’s implement the above steps in a Java program and check if the given number is a mystery number or not.

Mystery Number Java Program

MysteryNumberExample1.java

  1. import java.util.Scanner;  
  2. public class MysteryNumberExample1  
  3. {  
  4. //function that finds reverse of the given number  
  5. static int reverse(int x)  
  6. {  
  7. //converts the given number into string      
  8. String str = Integer.toString(x);  
  9. //stores string  
  10. String string=””;  
  11. for(int i=str.length()-1;i>=0;i–)  
  12. {  
  13. //stores the reverse of the string      
  14. string=string+str.charAt(i);  
  15. }  
  16. //converts the string into integer  
  17. int rev=Integer.parseInt(str);  
  18. //returns the reverse number  
  19. return rev;  
  20. }  
  21. //function that checks the number is mystery or not  
  22. static boolean isMysteryNo(int num)  
  23. {  
  24. for (int i=1; i <= num/2; i++)  
  25. {  
  26. //calling the function that reverse a number and assign it to j  
  27. int j = reverse(i);  
  28. //compares the sum of two numbers is equal to given number or not  
  29. if (i + j == num)  
  30. {  
  31. //prints a pair of numbers whose sum is the given number      
  32. System.out.println( i + ” ” + j);  
  33. System.out.println(num+ ” is a mystery number.”);  
  34. //returns a boolean value if pair is found  
  35. return true;  
  36. }  
  37. }  
  38. System.out.println(“The given number is not a mystery number.”);  
  39. //returns false if pair is not found  
  40. return false;  
  41. }  
  42. //driver code  
  43. public static void main(String args[])  
  44. {  
  45. Scanner sc=new Scanner(System.in);  
  46. System.out.print(“Enter a number: “);  
  47. //reading an integer from the user  
  48. int num = sc.nextInt();  
  49. //calling the user-defined function to check the number is a mystery or not  
  50. isMysteryNo(num);  
  51. }  
  52. }  

Output 1:

Enter a number: 154
59 95
154 is a mystery number.

Output 2:

Enter a number: 123
The given number is not a mystery number.

Note: The output may be different from above. The first output may also show 77 77 that also gives 154.

Leave a Comment