Guest viewing is limited

C# Help!


Mistress of the Night
Executive Leadership
Global Moderator
Sep 24, 2009
Reaction score
Hey guys... help me out if you've got some knowledge about C#! :eek:

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.

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;
                int neg = start;
                int pos = start;
                for (int i = 0; i < start; i++)
                    if (testPalindrome(start + i))
                        pos = start + i;
                return (start == neg) ? pos : neg;

        public static string ReverseString(string s)
            char[] arr = s.ToCharArray();
            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;
             return false;



I'm a bit lost, I'm no expert at this stuff!!!

Why on earth are you asking this here? lol? You're lucky I have the answer you need!

What you posted works, but to ignore the first number:

(A) just do start++ when you enter the function findNearPalindrome. Clearly you want to ignore the first number no matter what. start++ increments value in start by 1.


(B) Start your for loop from i=1.

However, you can make this more efficient by using the following method:
If your original number is say 9847, the next palindrome will be 98__ ... 98_9 ... 9889.

If your original number is say 37284, the next palindrome will be 37___ ...37__3 ... 37_73 ... 37373 (not 37273 because you want the next palindrome number).

This method makes it faster. You won't need to use the testPalindrome function.

For the love of god, please comment your code if you intend on putting this out somewhere!

I was ignored on another board; I thought I would actually get a response here!

It's shared code from somewhere else, I'm modifying it to put into something else I'm working. Funny thing this is, my job has nothing to do with programming, yet here I am doing this little side project. Hmm..

Thanks for your help, I think I've got it figured out now.

Much <3