Next Larger Palindrome of a given number in C#

Given an integer find the immediate larger integer which is a palindrome, example 1234212 -> 1234321, 345676 -> 346643.

class Next_Larger_Palindrome
{
static void Main(string[] args)
{
Console.WriteLine(“Please enter a number: “);
int num = Convert.ToInt32(Console.ReadLine());
while (true)
{
if (num.ToString().Equals(num.ToString().ReverseString()))
{
Console.WriteLine(string.Format(“Next larger PALINDROME number of given number is {0}.”, num));
break;
}
else
{
++num;
}
}
Console.ReadKey();
}
}

public static class stringFunctions
{
public static string ReverseString(this string str)
{
string resStr = string.Empty;
for (int i = str.Length – 1; i >= 0; –i)
{
resStr += str[i];
}
return resStr;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s