# Determining whether a number is a palindrome

Can someone improve this for me? It seems ugly.

The method should test whether an incoming number is a palindrome and return a Boolean result.

private static bool IsPalindrome(int value)
{
var inverseValue = string.Concat(value.ToString().Reverse());
return inverseValue == value.ToString();
}

• What's ugly? Looks pretty succinct to me. Commented Sep 6, 2013 at 0:03

Not much there to be ugly to be honest. I guess all I might consider would be the reverse of MrSmith42 and adding functionality to avoid the duplication of ToString().

private static bool IsPalindrome(int value)
{
var valueStr = value.ToString();
return valueStr == string.Concat(valueStr.Reverse());
}


Looks o.k. and clear to me. But you can reduce it to one line, if you want.

private static bool IsPalindrome(int value){
return string.Concat(value.ToString().Reverse()) == value.ToString();
}

• Clever one-liners are rarely the most readable way to go, imho. Commented Sep 11, 2013 at 23:45
• @retailcoder: I think this one-liner is quite easy to read. But I agree, you should always prefer easy to read variants. Commented Sep 12, 2013 at 9:35

Well it totally depends on what you want as a "clean" result.

If the conversion to string for the reverse bothers you, you can make an extension method to hide it :

public static class Extensions
{
public static int Reverse(this int input)
{
return Convert.ToInt32(string.Concat(input.ToString().Reverse()));
}
}


so the original method is reduced to :

private static bool IsPalindrome(int value)
{
return value == value.Reverse();
}


If you prefer Fluent expressions, you can use .Equals and make it a one liner :

private static bool IsPalindrome(int value)
{
return string.Concat(value.ToString().Reverse()).Equals(value.ToString());
}


If the duplicate ToString() calls bothers you, you can put it in a temporary variable :

private static bool IsPalindrome(int value)
{
string valueString = value.ToString();
string inverse = string.Concat(valueString.Reverse());

return inverse == valueString;
}


And of course if you want to leave it like that (and maybe change the var to string) it would be correct too, as the original method is truly not horrible.

Yes, you can do much better! You don't need to use string. I don't know C#, so feel free to edit my answer.

Here is an idea for a function:

function int reverse(int myNumber)
{
int lastDigit = myNumber % 10;
if (lastDigit == myNumber)
{
return myNumber;
}
else
{
int myNumberWithLastDigitDeleted = (myNumber - lastDigit) / 10;
return lastDigit + 10 * reverse(myNumberWithLastDigitDeleted);
}
}

• This answer is downvoted: I don't have the same opinion as you on what is elegant ! Converting an int to a string to test whether it is a palindrome is IMO not elegant at all! Commented Sep 11, 2013 at 22:47
• I don't know C# either, but converting an int to a string can be elegant if done well. I wasn't the one who downvoted you, by the way. Commented Sep 11, 2013 at 23:38
• Not my downvote either, but how is a cryptic, recursive function with variables named i, j and n anywhere near being more elegant than the OP's solution? Commented Sep 12, 2013 at 0:07
• I agree that it is cryptic. Maybe with other names it more clear ! What do you think of the following: to test whether a number is a multiple of 10, I convert it to a string, get the last character and test if it is equal to the character "0" ? Commented Sep 12, 2013 at 0:22
• +1 for using math on numbers instead of string manipulation. It would need to be benchmarked, but certainly a viable option. Commented Sep 7, 2014 at 2:17