0

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
           }
    }
}
user58736
  • 9
  • 8

1 Answers1

0

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.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • thank you! its embarrassing to see how small mistakes can be! there was also one more mistake which you indirectly informed me about! thank you so much & yes i should learn that :( have to stop being lazy – user58736 Feb 25 '17 at 20:23
  • just reply & i will delete this post ..no use of it now! – user58736 Feb 25 '17 at 20:25
  • Never delete your post unless it has too many negative votes. Your question doesn't have any negative votes which means that the SD users are happy with it. If you still delete it, I will lose some rep points that I earned by answering your question. – VHS Feb 25 '17 at 20:47
  • oh sorry, i was not aware of it... I just edited some small mistakes though! :) – user58736 Feb 25 '17 at 20:53