# Java Program to Check Mystery Number

In the previous article, we have discussed Java Program to Check Bouncy Number

In this article we are going to understand what Mystery number is and how we can check whether a number is Mystery or not in Java with examples.

## Program to Check Mystery Number

Mystery numbers are numbers which can be equally divide into two numbers which are reverse of one other.

 Example :

132: 93+39 Mystery number
154: 68+86 Mystery number
23: Not a Mystery number

In the above examples the numbers 132 and 154 are Mystery numbers as they can be divided into two numbers which are reverse of one another. However 23 not a Mystery number.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach :

1. We ask the user to enter a number and store it .
2. Then we run a loop of numbers from one to the half of the entered number, make sum of the loop iterator and its reverse.
3. If the sum is equivalent to the number, then the number is said to be a Mystery number.

### Program:

Let’s see the program to understand it clearly.

import java.util.Scanner;

public class MysteryNumber
{
public static void main(String args[])
{
//Taking the number as input from the user using scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();

int i,j;
boolean flag = false;
// Loop to run and check if any number and its reverse adds upto the entered number
for(i = 1; i<=num/2;i++)
{
j = revNum(i);
if(i+j==num)
{
flag = true;
break;
}
}

if(flag)
{
System.out.println(num+" = "+i+"+"+revNum(i));
System.out.println(num+" is a Mystery number");
}
else
{
System.out.println(num+" is Not a Mystery number");
}

}

// Function that returns the reverse of the number
static int revNum(int num)
{
int rem, rev = 0;
while(num>0)
{
rem = num%10;
rev = rem+ (rev*10);
num/=10;
}
return rev;
}
}


Output:

Case-1

Enter a number : 132
132 = 39+93
132 is a Mystery number

Case-2

Enter a number : 146
146 is Not a Mystery number

Are you new to the java programming language? We recommend you to ace up your practice session
with these Basic Java Programs Examples

Related Java Programs: