Description: The following program takes an integer as input and checks to see if the number is a palindrome, i.e. the number reads the same forward and backward (eg. 12321)
Primary Inputs: an Integer
Primary Output: Determine if the input number is a palindrome or not.
Platform Used: JDK 1.6 with Notepad.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.io.*; class Palindrome { public static void main(String[] args) { Console con = System.console(); System.out.print("Enter the number"); int num = Integer.parseInt(con.readLine()); if (isPalindrome(num)) System.out.println("the number is palindrome"); else System.out.println("the number is not palindrome"); } static boolean isPalindrome(int num) { int revNum = 0; int tempNum = num; while (tempNum > 0) { revNum = revNum * 10 + tempNum % 10; tempNum = (tempNum - tempNum % 10) / 10; } if (revNum == num) return true; return false; } } |