Advertisement
therrontelford

Palindrome

Nov 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Chap3_12Palindrome {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner kb = new Scanner(System.in);
  7.         System.out.println("Enter a 3 digit number");
  8.  
  9.         // get user input
  10.         int number = kb.nextInt();
  11.  
  12.         // I created this variable to extract the digits so that I would not change the value stored in "number"
  13.         int num = number;
  14.  
  15.        
  16.         int ones = num % 10;  // the remainder is the ones digit
  17.         num = num/10;   // integer division gets rid of the ones digit. I have a new num
  18.         int tens = num % 10;  // the remainder is the ones digit again.  This gives me the middle number of the original number
  19.         num = num / 10;   // integer division gets rid of the last digit.  I have a new num again.
  20.         int hundreds = num;  // the only digit remaining.  This is the hundreds
  21.        
  22.         if (ones == hundreds)
  23.             System.out.println(number + " is a palindrome");
  24.         else
  25.             System.out.println(number + " is not a palindrome");
  26.        
  27.  
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement