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#
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