I am creating a console application to find the nearest palindrome number to the one submitted, for the life of me I cannot figure out how to make it find the next highest palindrome it will return the same number if that is a palindrome and also will literally do the closest even if it is less.
I would really appreciate any help.
namespace PalidromeC
{
class Program
{
static public void Main(string[] args)
{
int palindrome = NearPalindromeFinder.findNearPalindrome(101);
Console.WriteLine("Nearest Palindrome = " + palindrome);
}
}
public class NearPalindromeFinder
{
public static int findNearPalindrome(int start)
{
if (testPalindrome(start))
return start;
else
{
int neg = start;
int pos = start;
for (int i = 0; i < start; i++)
{
if (testPalindrome(start + i))
{
pos = start + i;
break;
}
}
return (start == neg) ? pos : neg;
}
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
private static bool testPalindrome(int start)
{
if (start == 0 || start == 1)
return true;
String str = Convert.ToString(start);
String rev = ReverseString(str);
if (str.Equals(rev))
return true;
else
return false;
}
}
}
Hehe, Outer Worlds was the first game that was like "We've noticed you've been dying a lot recently, do you want to get this free perk that will make you die less, but will make your stamina go down faster by 7% ?"
One of better games I've ever played and I actually found it by chance on Reddit or something. Didn't know it existed, it was released for a while at that point and I decided to give it a try since I like RPG's with guns more over RPG's with swords and bows. And I absolutely loved it so I can't wait for its sequel. After disappointment of Starfield that was too big and too generic, Outer Worlds 2 will hopefully just be a bit larger single solar system with unique planets in it just like the first one. It was such a good concept. Year 2025 is really loaded with so many awesome games and more to come!
Question
han84
Hi All,
I am creating a console application to find the nearest palindrome number to the one submitted, for the life of me I cannot figure out how to make it find the next highest palindrome it will return the same number if that is a palindrome and also will literally do the closest even if it is less.
I would really appreciate any help.
Link to comment
https://www.neowin.net/forum/topic/884304-c-find-nearest-palindrome/Share on other sites
7 answers to this question
Recommended Posts