Use it to reverse a given number. Data types are declared as BigIntegers to use this function to find a palindrome given a number.
BigInteger is available in .NET Framework 4.
You need a reference to System.Numerics name space to use this datatype.
private BigInteger ReverseNumber(BigInteger n)
{
BigInteger left = n;
BigInteger rev = 0;
BigInteger r;
while (left > 0)
{
r = left % 10;
rev = rev * 10 + r;
left = left / 10; //left = Math.floor(left / 10);
}
return rev;
}
If you wish to read a number as a string, the following would be helpful.
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
No comments:
Post a Comment