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

I am constructing a program to detect palindromes in java. For some reason the p

ID: 3762235 • Letter: I

Question

I am constructing a program to detect palindromes in java. For some reason the program will only run through one loop could you please tell me why that is.

public class Hi {
public static void main(String[] args) {
boolean again=true;
int in= Integer.parseInt((args[0]));
int smiley=in;
int re;
int plus;
int counter=0;
while (pal(in)==false);
{
plus=sum(in,rev(in));
if (pal(plus)==true)
   {
in=plus;
   counter ++;
}
else
{
in=plus;
counter ++;
System.out.println(in);
      
}
  
}
System.out.println("The number "+smiley+" took "+counter+" steps to reach a palindrome");
}
public static int rev(int in)
{
int re=0;
while(in !=0)
{
re=re*10;
re = re+ in%10;
in = in/10;
}
return re;
}
public static boolean pal(int in)
{ int re=rev(in);
int plus=re+in;
int pleasegod=rev(plus);
  
if (pleasegod==rev(pleasegod))
return true;
else return false;
}

public static int sum(int in, int re)
{
return in+re;
}

}

Explanation / Answer

Reasons
1. Intially while loop is not declared as while (pal(in)==false);(not ended with semi colon)
2. This while loop is terminate with entering into body
then the pal() method is calls only once
3. This app can be used to generate palindrome numbers by doubling the given number
use this code to do that

public class Hi {
   public static void main(String[] args) {
       boolean again = true;
       int in =Integer.parseInt((args[0]));//121
       int smiley = in;
       int re;
       int plus;
       int counter = 0;
//       boolean flag=pal(in);
//       System.out.println("flag=="+flag);
       while (pal(in) == true) // changed the false to true and removed semicolon
          
       {
           plus = sum(in, rev(in));
           System.out.println("plus="+plus);
           if (pal(plus) == true) {
               in = plus;
               counter++;
           } else {
               in = plus;
               counter++;
               System.out.println(in);

           }
           //System.out.println("in=="+in);

       }
       System.out.println("The number " + smiley + " took " + counter
               + " steps to reach a palindrome");
   }

   public static int rev(int in) {
       //System.out.println("rev method :"+in);
       int re = 0;
       while (in != 0) {
           re = re * 10;
           re = re + in % 10;
           in = in / 10;
       }
       return re;
   }

   public static boolean pal(int in) {
//       System.out.println("pal method in:"+in);
       int re = rev(in);
       int plus = re + in;
      
       int pleasegod = rev(plus);
       /*System.out.println("re "+re);
       System.out.println("plus "+plus);
       System.out.println("pleasegod "+pleasegod);*/

       if (pleasegod == rev(pleasegod))
           return true;
       else
           return false;
   }

   public static int sum(int in, int re) {
       return in + re;
   }
}

test for the input 121
plus=242
plus=484
484
The number 121 took 2 steps to reach a palindrome