Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Chap3_12Palindrome {
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.println("Enter a 3 digit number");
- // get user input
- int number = kb.nextInt();
- // I created this variable to extract the digits so that I would not change the value stored in "number"
- int num = number;
- int ones = num % 10; // the remainder is the ones digit
- num = num/10; // integer division gets rid of the ones digit. I have a new num
- int tens = num % 10; // the remainder is the ones digit again. This gives me the middle number of the original number
- num = num / 10; // integer division gets rid of the last digit. I have a new num again.
- int hundreds = num; // the only digit remaining. This is the hundreds
- if (ones == hundreds)
- System.out.println(number + " is a palindrome");
- else
- System.out.println(number + " is not a palindrome");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement