Reputation: 11237
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?
Upvotes: 1
Views: 766
Reputation: 701
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
.
Upvotes: 1
Reputation: 54543
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);
Upvotes: 2