Mystery Number in Java

In this section, we will learn what is a mystery number and also create Java programs 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

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.