Write a java program to check whether a number is palindrome or not using while loop in Java.
Java Palindrome Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter a Number:"); int num = scanner.nextInt(); int reversedInteger = 0, remainder, originalInteger; originalInteger = num; // reversed integer is stored in variable for( ;num != 0; num /= 10 ) { remainder = num % 10; reversedInteger = reversedInteger * 10 + remainder; } // palindrome if orignalInteger and reversedInteger are equal if (originalInteger == reversedInteger) System.out.println(originalInteger + " is a palindrome."); else System.out.println(originalInteger + " is not a palindrome."); } |
Output: