1.11M Members

5 digit palindrome program

 
0
 

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

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("Enter a five digit number");  // prompt 
                  number = input.nextInt();
                  int i=number;
                  while(i>0)
                  {
         digits++;
          i/=10;
    }
while(digits!=5) //if the number is not five digits
{
 System.out.println("The number you entered is not a five-digit number \nPlease Enter a five digit number:");//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 && digit2==digit4) //tests whether the first and last digits are identical and the second and Fourth digits are identical.
         System.out.println(number + " is a plaindrome!");//Output whether its a palindrome
         else
         System.out.println(number + " is  not a plaindrome!");

   } // end method checkPalindrome
} // end class Palindrome
 
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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article