1

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?

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75

2 Answers2

2

I think you can just reverse the string rather than treat it as a number:

using System.Linq;

var N = textBox1.Text;
var reversedN = new string(n.Reverse().ToArray());
var result = Convert.ToInt32(reversedN);
zs2020
  • 53,766
  • 29
  • 154
  • 219
1

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.

Community
  • 1
  • 1
Michi
  • 681
  • 1
  • 7
  • 25