A palindrome number in Java is a number that when read in reverse order is the same as read in the right order.
In other words, a palindrome number is a number that remains the same after reverse. For example: 53235, 171, 12321, 151, 101, etc.
These are all palindrome numbers. If we reverse it and read after reversing, the number remains the same.
Basic Steps to check a Number is Palindrome or not:
- Get the number from the user to check palindrome.
- Assign the original number to another new variable.
- Then, reverse number stored in the new variable. Apply loop and write the logic to reverse the number.
- Now, compare the reversed number with the number stored into a new variable using == operator.
- If both the numbers are the same, then display the message as “Number is a palindrome”.
- Otherwise, display a message as “Number is not palindrome”.
Number Palindrome Program in Java
1. Let’s write a program to check whether an input number is palindrome or not. In this palindrome program, we will take an input as number from the user using Java Scanner class.
Program code 1:
package javaProgram; import java.util.Scanner; public class PalNumber { public static void main(String[] args) { int num, newNum, reverse = 0, remainder; // variables declared. // Create a Scanner class to take the input from the user. Scanner sc = new Scanner(System.in); // Prompt the user to enter a number. System.out.println("Enter any number: "); num = sc.nextInt(); newNum = num; // Assigning original integer number to another one. // Reverse the integer number stored in a variable newNum. while(num > 0) { remainder = num % 10; // getting remainder. reverse = (reverse * 10) + remainder; num = num / 10; } if(reverse == newNum) System.out.println("Number is Palindrome"); else System.out.println("Number is not Palindrome"); } }
Output: Enter any number: 121 Number is Palindrome Enter any number: 1256 Number is not Palindrome
Explanation:
(a) In this palindrome program, the condition 121 > 0 is true, therefore, remainder = 121 % 10 = 1 and reverse = (0 * 10) + 1 = 1.
(b) Now, number 121 divides by 10, and we get 12 that is greater than 0. This number 12 become stored into the variable num.
(c) Since 12 > 0, remainder = 12 % 10 = 2. Now, reverse = (1 * 10) + 2 = 12.
(d) The number 12 stored in the variable num again divides by 10 (12/10) and we get the quotient 1. This number 1 becomes stored in the variable num.
(e) Since num 1 is greater than zero, remainder = 1 % 10 = 1. The reverse = (12 * 10) + 1 = 121.
(f) Now, reverse number is 121 and original number is also 121.
(g) We have compared the reverse number with original number using double equal operator.
(h) Since both numbers are equal, number 121 is a palindrome.
2. Let’s take another example program based on palindrome, in which we will check a number is palindrome or not by using method of String class.
Program code 2:
package javaProgram; import java.util.Scanner; public class PalNumber { public static void main(String[] args) { String str, revString = ""; // Objects of String class. // Create a Scanner class to take input from keyboard. Scanner sc = new Scanner(System.in); // Prompt the user to enter a number to check palindrome. System.out.println("Enter a number to check whether it is a palindrome or not: "); str = sc.nextLine(); // Find the length of string. int len = str.length(); for ( int i = len - 1; i >= 0; i-- ) revString = revString + str.charAt(i); if (str.equals(revString)) System.out.println("Entered number is a palindrome."); else System.out.println("Entered number isn't a palindrome."); } }
Output: Enter a number to check whether it is a palindrome or not: 53235 Entered number is a palindrome. Enter a number to check whether it is a palindrome or not: 1234 Entered number isn't a palindrome.
In this tutorial, you learned palindrome number in Java with example programs. Hope that you will have understood how to check whether a number is a palindrome or not.
Thanks for reading!!!
Next ⇒ How to sort String in Java Alphabetically⇐ PrevNext ⇒