Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package eecs.hm; public class M1 { /* Given a positive integer n, return the num

ID: 3808013 • Letter: P

Question

 package eecs.hm;  public class M1 {     /*     Given a positive integer n, return the number of "palindro additions" needed     to make it a palindrome (i.e. reads the same left-to-right and right-to-left).     A "palindro addition" is the addition of the number to its inverse.      For example, if n = 159 then the first palindro addition yields 159+951=1110,     and the second palindro addition yields 1110+111=1221, which is a palindrome.     Hence, the palindro count of 159 is 2.      If n is already a palindrome then return zero, and if the number does not     become a palindrome after 10 palindro additions then return -1.      */     public static int palindroCount(int n)     {         return -1; // delete me!     } } 

Explanation / Answer


public class M1 {

   public static void main(String[] args) {
       System.out.println("Palindro count -- >" + palindroCount(152, 1));
   }

   public static int palindroCount(int n, int count) {
       if(count ==1 && n== reverseNumber(n)){
           return 0;
       }
       else if (count< 10) {
           int k = n + reverseNumber(n);
           int temp = reverseNumber(k);
           if (k == temp) {
                  System.out.println("Palidro Num:"+k);
               return count;
           } else {
               return palindroCount(k, count+1);
           }
          
       }else {
           return -1;
       }
      
   }

/*function to reverse the given number*/

   private static int reverseNumber(int input) {
       StringBuffer buf = new StringBuffer(Integer.toString(input));
       int reversedNum = Integer.parseInt(buf.reverse().toString());
       return reversedNum;
   }
}

Sample Output:

Palidro Num:707

Palindro count -- >2