Search code examples
c#palindrome

How do I find the palindrome of a number?


I already wrote the part of the program that reverses the number:

        int n = 0;
        n = Convert.ToInt32(textBox1.Text);
        double l = n;
        double reverse = 0;
        while (left > 0)
        {
            double r = left % 10;
            reverse = reverse * 10 + r;
            l = l / 10;
        }

        double final = n + rev;

However, if the final double does is not a palindrome, I want the program to add the final to its palindrome, and keep doing that until the a palindrome is found. The problem is in the the conditional statement.What condition should I give to the if statement?


Solution

  • As in this thread

    bool isPalindrome(String s)
    {
        char[] charArray = s.ToCharArray();
        Array.Reverse( charArray );
        return s.equals(new string(charArray));
    }
    

    Do the palindrome check on the string. If it's not a palindrome, convert the string to an int, add whatever you want, convert it back to a string, repeat.

    Avoid working with double in this application. Stick to int.