# Check if a number is a Mystery Number

Given a number check whether it is a mystery number or not. A mystery number is that number which can be expressed as sum of two numbers and those two numbers should be reverse of each other.

Examples:

Input : n = 121
Output : 29 92

Input : n = 22
Output : 11 11

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Source :Paytm Interview Set 23

The idea is to try every possible pair smaller than or equal to n.

Below is the implementation of above approach.

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Finds reverse of given num x. ` `int` `reverseNum(``int` `x)  ` `{ ` `    ``string s = to_string(x); ` `    ``reverse(s.begin(), s.end()); ` `    ``stringstream ss(s);  ` `    ``int` `rev = 0;  ` `    ``ss >> rev; ` `    ``return` `rev;  ` `} ` ` `  `bool` `isMysteryNumber(``int` `n) ` `{ ` `    ``for` `(``int` `i=1; i <= n/2; i++)  ` `    ``{ ` `        ``// if found print the  pair, return  ` `        ``int` `j = reverseNum(i); ` `        ``if` `(i + j == n)  ` `        ``{ ` `            ``cout << i << ``" "` `<< j;  ` `            ``return` `true``; ` `        ``} ` `    ``} ` ` `  `    ``cout << ``"Not a Mystery Number"``; ` `    ``return` `false``; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `n = 121; ` `    ``isMysteryNumber(n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` ` `  `class` `GFG ` `{ ` `    ``// Finds reverse of given num x. ` `    ``static` `int` `reverseNum(``int` `x)  ` `    ``{ ` `        ``String s = Integer.toString(x); ` `        ``String str=``""``; ` `        ``for``(``int` `i=s.length()-``1``;i>=``0``;i--) ` `        ``{ ` `             `  `            ``str=str+s.charAt(i); ` `        ``} ` `         `  `        ``int` `rev=Integer.parseInt(str); ` `        ``return` `rev;  ` `    ``} ` `     `  `    ``static` `boolean` `isMysteryNumber(``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=``1``; i <= n/``2``; i++)  ` `        ``{ ` `            ``// if found print the pair, return  ` `            ``int` `j = reverseNum(i); ` `            ``if` `(i + j == n)  ` `            ``{ ` `                ``System.out.println( i + ``" "` `+ j);  ` `                ``return` `true``; ` `            ``} ` `        ``} ` `     `  `         ``System.out.println(``"Not a Mystery Number"``); ` `        ``return` `false``; ` `    ``} ` `     `  `    ``public` `static` `void` `main(String []args) ` `    ``{ ` `        ``int` `n = ``121``; ` `        ``isMysteryNumber(n); ` `     `  `    ``} ` ` `  `} ` ` `  `// This code is contributed by ihritik `

## Python3

 `# Python3 implementation of above approach ` ` `  `# Finds reverse of given num x. ` `def` `reverseNum(x): ` `    ``s ``=` `str``(x) ` `    ``s ``=` `s[::``-``1``] ` `    ``return` `int``(s) ` `     `  `def` `isMysteryNumber(n): ` `     `  `    ``for` `i ``in` `range``(``1``, n ``/``/` `2` `+` `1``): ` `         `  `        ``# if found print the pair, return ` `        ``j ``=` `reverseNum(i) ` `         `  `        ``if` `i ``+` `j ``=``=` `n: ` `            ``print``(i, j) ` `            ``return` `True` `     `  `    ``print``(``"Not a Mystery Number"``) ` `    ``return` `False` ` `  `# Driver Code ` `n ``=` `121` `isMysteryNumber(n) ` ` `  `# This code is contributed by  ` `# Mohit Kumar 29 (IIIT gwalior) `

## C#

 `// C# implementation of above approach ` ` `  `using` `System; ` `class` `GFG ` `{ ` `    ``// Finds reverse of given num x. ` `    ``static` `int` `reverseNum(``int` `x)  ` `    ``{ ` `        ``string` `s = x.ToString(); ` `        ``string` `str=``""``; ` `        ``for``(``int` `i=s.Length-1;i>=0;i--) ` `        ``{ ` `             `  `            ``str=str+s[i]; ` `        ``} ` `         `  `        ``int` `rev=Int32.Parse(str); ` `        ``return` `rev;  ` `    ``} ` `     `  `    ``static` `bool` `isMysteryNumber(``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=1; i <= n/2; i++)  ` `        ``{ ` `            ``// if found print the pair, return  ` `            ``int` `j = reverseNum(i); ` `            ``if` `(i + j == n)  ` `            ``{ ` `                ``Console.WriteLine( i + ``" "` `+ j);  ` `                ``return` `true``; ` `            ``} ` `        ``} ` `     `  `        ``Console.WriteLine(``"Not a Mystery Number"``); ` `        ``return` `false``; ` `    ``} ` `     `  `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 121; ` `        ``isMysteryNumber(n); ` `     `  `    ``} ` ` `  `} ` ` `  `// This code is contributed by ihritik `

## PHP

 ` `

Output:

```29 92
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

9

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.