Java Palindrome

Mountain State University Java Assignment

3.30 (Palindromes)

Write an application that reads in a five digit integer and determines whether it’s a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value.

Java Palindrome Source Code

import java.util.Scanner; //initialize scanner

public class palindrome // defines class name
{

public static void main( String[] args )
{
// Scanner = new Scanner( System.in );
Scanner input = new Scanner( System.in );

int digit; // enter 5 digits

System.out.print( "Please enter a 5 digit number \n" );
digit = input.nextInt();
while (digit < 10000 || digit > 99999)
{
System.out.printf("Please enter a 5 digit number only \n");
System.out.print( "Enter a 5 digit number \n " );
digit = input.nextInt();
}

if ( digit % 10 / 1 == digit % 100000 / 10000 )
{
if ( digit % 100 / 10 == digit % 10000 / 1000 )
System.out.printf( "Your number is a palindrome." );
else
System.out.printf( "Your number is not a palindrome." );
}
else
System.out.printf( "Your number is not a palindrome." );
} // end main method

} // end class

0
  Related Posts