/*
  File: PalindromicSum.java
  Description: To find the two-digit number which takes the most additions of its reverse to reach a palindromic sum.
  Student Name: Evan Wilson
  Student UT EID: esw237
  Course Name: CS 303E
  Unique Number: 92080
  TA: Jonathan Mugan
  Date Created: 31 July 2006
  Date Last Modified: 3 August 2006
*/

public class PalindromicSum
{
	public static boolean isPalindromic (long n)
	{
		// Convert long n to String num in order to check if long n is palindromic.
		String num = String.valueOf (n);
	      int len = num.length();
	      num.charAt (0);
        
		for (int c = 0; c < len / 2; c++)
      	{
      		char a = num.charAt (c);
	            char b = num.charAt (len - 1 - c);
	            if (a != b)
	            	return false;
      	}
	      return true;
	}
    
      public static void main (String[] args)
      {
      	long maxSteps = 0;
	      long maxNum = 0;
		long [] steps = new long[100];
        
		// Step through numbers 10 to 99.
            for (int i = 10; i < 99; i++)
            {
            	long counter = 0;
	            long j = i;

      	      while (!isPalindromic (j))
            	{
	   	      	// Convert j to array num in order to get the reverse of j.
	                  String num = String.valueOf (j);
	                  StringBuffer buffer = new StringBuffer (num);
			      buffer.reverse();
	                  num = buffer.toString();
				long rev = Long.parseLong(num);	          
				j += rev;
	                  counter++;
       	      }
			steps[i] = counter;

         	      if (counter > maxSteps)
          		{
                 		maxSteps = counter;
                        maxNum = i;
                  }
        	}
        	System.out.println("The number of steps is: " + maxSteps + ".");
        	System.out.print("The numbers are: ");
		for (int i = 0; i < steps.length; i++)
		{
	  		if (steps[i] == maxSteps)
	   			System.out.print(i +", ");
		}
    	}
}