Number palindrome checker My daughter, nine, recently had some homework looking at number palindromes. Part of the assignment was to take a number, reverse it, add the two together and repeat until she got a palindrome and to do this for several different numbers.

While helping her with this it struck me it'd be a nice simple idea for a computer program. She &amp; I have messed about in Scratch a bit, but this sounded like a nice problem to do in a more commercial language. Thought I could write it and then show her how it worked.

So I did. It turned out to be not quite as child-friendly as I'd imagined.

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace CheckNumberPalindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            string rollingSum;
            int iterations = 1;

            if(IsNumeric(args[0]) == false)
            {
                Console.WriteLine("You did not enter a numeric value");
            }

            rollingSum = RecurseUntilPalindrome(args[0], ref iterations);
            Console.WriteLine(string.Format("The final palindrome is {0}, which took {1} steps.", rollingSum, iterations));
        }

        private static bool IsNumeric(string s)
        {
            long result;
            return Int64.TryParse(s, out result);
        }

        private static string RecurseUntilPalindrome(string s, ref int iterations)
        {
            long rollingSum = AddToPalindrome(s);

            if(IsPalindrome(rollingSum.ToString()))
            {
                return rollingSum.ToString();
            }

            iterations++;
            return RecurseUntilPalindrome(rollingSum.ToString(), ref iterations);
        }

        private static long AddToPalindrome(string s)
        {
            string reversedString = ReverseString(s);
            long result = Convert.ToInt64(s) + Convert.ToInt64(reversedString);
            Console.WriteLine(string.Format("Adding {0} to {1} = {2}", s, reversedString, result));
            return result;
        }

        private static string ReverseString(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

        private static bool IsPalindrome(string s)
        {
            return s.SequenceEqual(ReverseString(s));
        }
    }
}


There are two questions here:


How could I make this better or cleaner or more efficient or any of the other things that CR does so well?
What could I do (beside hiding some of the functions in pre-compiled code elsewhere) to make this more child-friendly, or better for teaching/explaining some basic code concepts?
This may help with your second point about code concepts:

How about addressing the special case for when the user enters a palindrome?  It looks to me like you are assuming the entered number will not be a palindrome and you automatically add to it (calling RecurseUntilPalindrome).  How about adding an initial check to account for that case?  I think it would go along well with your numerical value input check (I consider invalid user input as a sort of special case as well).

To address your first point, I think your code looks pretty clean as is! Really easy to follow.