954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

5 digit palindrome program

Hello people ..

I have a problem with my program ,, it is designed to count the number of digits and if its 5 it will test if its a palindrome number..

my problem is with the while loop... please help

<code>public class Palindrome
{
   // checks if a 5-digit number is a palindrome
   public void checkPalindrome()
   {
      Scanner input = new Scanner( System.in );

      int number; // user input number
      int digit1; // first digit
      int digit2; // second digit
      int digit4; // fourth digit
      int digit5; // fifth digit
      int digits; // number of digits in input

      number = 0;
      digits = 0;
		int a, b, c;

         System.out.println(&quot;Enter a five digit number&quot;);  // prompt 
                  number = input.nextInt();
                  int i=number;
                  while(i&gt;0)
                  {
	     digits++;
	      i/=10;
	}
while(digits!=5) //if the number is not five digits
{
 System.out.println(&quot;The number you entered is not a five-digit number \nPlease Enter a five digit number:&quot;);//Display an error message
						  number=input.nextInt();
}
						  
digit1=number/10000;
a=number%10000;
digit2=a/1000;
b=a%1000;
c=b%100;
digit4=c/10;
digit5=c%10;

if(digit1 == digit5 &amp;&amp; digit2==digit4) //tests whether the first and last digits are identical and the second and Fourth digits are identical.
         System.out.println(number + &quot; is a plaindrome!&quot;);//Output whether its a palindrome
         else
         System.out.println(number + &quot; is  not a plaindrome!&quot;);

   } // end method checkPalindrome
} // end class Palindrome</code>
SQ89
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

There are two while loops in your program and both are looking fine to me. Trace through your program putting debugging statements and check the values for i and digits.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You