Mystery Number in Java

In this section, we will explore the concept of mystery numbers and develop Java programs to determine whether a given number is a mystery number. The topic of mystery numbers often appears in Java coding tests and academic assignments. Our focus will be on understanding what constitutes a mystery number and implementing code to check for its presence in a given number.

What is a Mystery Number

A mystery number refers to a number, denoted as N, that can be represented as the sum of two numbers. Notably, these two numbers must be reverses of each other. The range of mystery numbers lies between 22 and 198, inclusively (22 <= N <= 198).

The term “mystery numbers” originates from their connection to multiples of 11. In the decimal numeration system, the sum of the place values of the units (1) and tens (10) place is always 11.

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

Given a number that is split into two two-digit numbers by interchanging its unit and tens digits, we need to determine the mystery number.

Let y represent the unit digit and X represent the tens digit of the original number. The original number can then be represented as 10X + y. After interchanging the unit and tens digits, we obtain the number 10y + X. Therefore, the mystery number is given by the expression:

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 the condition is given in two-digit numbers.

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

Steps to Find Mystery Number

  1. Read or assign a value to the variable N.
  2. Convert the given number N into a string.
  3. Find the reverse of the string.
  4. Convert the reversed string back into an integer.
  5. Sum up the pair of numbers.
  6. Compare the sum with the given number.
  7. If the sum and the given number are equal, then the number is a mystery number.
  8. Otherwise, it is not a mystery number.

Mystery Number Java Program

MysteryNumberExample.java

import java.util.Scanner;  
public class MysteryNumberExample  
{  
//function that finds reverse of the given number  
static int reverse(int x)  
{  
//converts the given number into string      
String str = Integer.toString(x);  
//stores string  
String string="";  
for(int i=str.length()-1;i>=0;i--)  
{  
//stores the reverse of the string      
string=string+str.charAt(i);  
}  
//converts the string into integer  
int rev=Integer.parseInt(str);  
//returns the reverse number  
return rev;  
}  
//function that checks the number is mystery or not  
static boolean isMysteryNo(int num)  
{  
for (int i=1; i <= num/2; i++)  
{  
//calling the function that reverse a number and assign it to j  
int j = reverse(i);  
//compares the sum of two numbers is equal to given number or not  
if (i + j == num)  
{  
//prints a pair of numbers whose sum is the given number      
System.out.println( i + " " + j);  
System.out.println(num+ " is a mystery number.");  
//returns a boolean value if pair is found  
return true;  
}  
}  
System.out.println("The given number is not a mystery number.");  
//returns false if pair is not found  
return false;  
}  
//driver code  
public static void main(String args[])  
{  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter a number: ");  
//reading an integer from the user  
int num = sc.nextInt();  
//calling the user-defined function to check the number is a mystery or not  
isMysteryNo(num);  
}  
}

Output 1:

Enter a number: 14
7 7
14 is a mystery number.

Output 2:

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

Expand your understanding of Java concepts, such as Mystery Number in Java, by visiting tutorials.freshersnow.com.