We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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
2
Contributors
1
Reply
12 Minutes
Discussion Span
4 Years Ago
Last Updated
3
Views
SQ89
Newbie Poster
19 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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: 91
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds using 2.84MB