javapalindromecall-by-value

Keep Reversing the no. & adding it to the original no. until there is a Palindrome Number


It's not "Find the Palindrome of a Number" ! it's a bit twisted than that:

Step 1: Reverse the digits of a original number.

Step 2: Add the reversed number.

Step 3: If the new number is a palindrome number then print the output.

The limit is 15 steps & we can print the original number if that is a palindrome in itself. We have to use function calling in this program. Here I have used two functions() - FIRST to reverse the number & SECOND to add the original number and the reversed number & check if the sum is a Palindrome or not.

Please suggest your opinions if I can improve this long code in any way.

PS: newbie in Java & especially function calling!

Thank you!! Here's the code, it's a little bit long! I am sorry :(

import java.util.*;
public class PalindromicNumber
{
 public int PalinReverse(int n)
    {
     int n1=n;
     int x1=0, d1=0;
      while (n1>0)//just used to store the reverse of the original number
        {
          d1=n1%10;
          x1=(x1*10)+d1;
          n1=n1/10;
        }
    return x1;
     }

    public int PalinCheck (int n, int p)
      {
         int F=0;
         F=n+p; 
         int n1=F, x1=0, d1=0;

          while(n1>0)//checks if the sum of reversed no. and the original number is palindrome or not
           {
            d1=n1%10;
            x1=(x1*10)+d1;
            n1=n1/10;
           }
   if (x1==F)
       {
       System.out.println("The number"+ F +"is a Palindrome");
       return 1;
       }
   else 
       return F; //returns the sum if it is not a palindrome
    }

   public static void main (String args[])
     {
      Scanner sc=new Scanner(System.in);
      PalindromicNumber ob=new PalindromicNumber();

      System.out.println("Enter the original number");
      int n=sc.nextInt();

      int count=0;
      int n1=n, x1=0, d1=0;
        while(n1>0) //this checks if the original no. is a palindrome or not
           {
            d1=n1%10;
            x1=(x1*10)+d1;
            n1=n1/10;
           }

        if (x1==n)
        System.out.println("The original number="+n+"is a palindrome number");

        else

        for (count=0;count<15;count++)
           {
               int a=ob.PalinReverse(n);
               System.out.println("The reversed number is"+a);
               int b=ob.PalinCheck(n,a);
                if(b==1)
                {
                    System.out.println("The no. of steps it took was"count+1);
                    break;// the palindromic no. is now found out
                } 
                else 
                     n=b;//used to update the value of n
           }
    }
}

Solution

  • The problem is in your PalinReverse method. Your while loop is running infinitely because the condition is always true.

    Replace

    while (n>0)

    with

    while (n1>0)

    You should also learn how to debug a program. Your life will be 10 times easy after that.