In this tutorial, we will write a c# program to check if a number is a Mystery Number. Before that, we may go through the following topic in c#.
A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. It lies between 22 to 198, i.e. 22<=N<=198.
Example:
132 = 93+39
154 = 68 + 86
176 = 79 + 97
Program to check Mystery Number in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System; class MysteryNumber { 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++) { int j = reverseNum(i); if (i + j == n) { Console.WriteLine( i + " " + j); Console.WriteLine("It is a Mystery Number"); return true; } } Console.WriteLine("It is not a Mystery Number"); return false; } public static void Main() { int n = 154; isMysteryNumber(n); } } |
Output:
59 95
It is a Mystery Number