// Lab 2: Palindrome.java
// Program tests for a palindrome
import java.util.Scanner;
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 digit3;
int digit4; // fourth digit
int digit5; // fifth digit
int digits; // number of digits in input
number = 0;
digits = 0;
/* Write code that inputs a five-digit number. Display an error message
if the number is not five digits. Loop until a valid input is received. */
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
while (number < 10000 && number > 99999)
{
System.out.println("Incorrect input, please try again...");
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
}
/* Write code that separates the digits in the five digit number. Use
division to isolate the left-most digit in the number, use a remainder
calculation to remove that digit from the number. Then repeat this
process. */
digit1 = number / 10000;
digit2 = number / 1000 % 10;
digit3 = number % 1000 / 100 % 10;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;
/* Write code that determines whether the first and last digits are
identical and the second and Fourth digits are identical. Output
whether or not the original string is a palindrome. */
if (digit1 == digit5 && digit2 == digit4)
System.out.printf("%d is a palindrome", number);
else
System.out.printf("%d is not a palindrome", number);
} // end method checkPalindrome
} // end class Palindrome
5 digit integer palindrome
Page 1 of 12 Replies - 2137 Views - Last Post: 14 March 2011 - 08:21 PM
#1
5 digit integer palindrome
Posted 14 March 2011 - 07:49 PM
having trouble with the while loop. i'm supposed to check to make sure that the entered integer is actually a five digit number. it complies and runs fine except for the loop. thanks in advance for any help or advice...
Replies To: 5 digit integer palindrome
#2
Re: 5 digit integer palindrome
Posted 14 March 2011 - 08:12 PM
if(number < 10000 || number > 99999)
... invalid input
not &&
... invalid input
not &&
#3
Re: 5 digit integer palindrome
Posted 14 March 2011 - 08:21 PM
its always something simple... thanks
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|