Overall your code looks very nice, readable and maintainable. I only have a few remarks:
if(IsNumeric(args[0]) == false)
{
Console.WriteLine("You did not enter a numeric value");
}
rollingSum = RecurseUntilPalindrome(args[0], ref iterations);
Why continue the program when the input is invalid (not numeric)? Check for a numeric value and only continue if the input is a number:
if(IsNumeric(args[0]))
{
//Continue here...
}
else
{
Console.WriteLine("You didn't enter a valid number!");
}
- Format in
Console.WriteLine:
Following line:
Console.WriteLine(string.Format("The final palindrome is {0}, which took {1} steps.", rollingSum, iterations));
can be rewritten as:
Console.WriteLine("The final palindrome is {0}, which took {1} steps.", rollingSum, iterations);
No need to call the String.Format.
While writing previous points I found that there had to be an easier way to achieve the same result. There are numerous ways to check for a palindrome. For numbers you can also use this method:
public static bool IsPalindrome(Int64 input)
{
Int64 reverse = 0;
var temp = input;
while (temp != 0)
{
var rem = temp % 10;
reverse = reverse * 10 + rem;
temp /= 10;
}
return reverse == input;
}
Thanks to using this method, there's no for creating strings and arrays and again conversions to numbers. Your entire code can be written as follows:
Int64 startNumber;
var steps = 1;
var input = Console.ReadLine();
if (Int64.TryParse(input, out startNumber))
{
Int64 reversed;
while(!IsPalindrome(startNumber, out reversed))
{
steps++;
var temp = startNumber + reversed;
Console.WriteLine("Adding {0} to {1} = {2}", reversed, startNumber, temp);
startNumber = temp;
}
Console.WriteLine("The final palindrome is {0}, which took {1} steps.", startNumber, steps);
}
else
{
Console.WriteLine("You didn't enter a valid number!");
}