In java, to check number is palindrome or not we can use loop concepts such as while loop or for loop.
Palindrome number is defined as a number that is same even after reversing it. For example, 131 is a palindrome number. Writing from left to right or right to left will give same number which is 131.
In this article we will learn writing program with the help of example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We need to write a java program to check number is palindrome or not. The program should take an integer number and then reverse the number and identify if it is a palindrome number or not and output the result. Let’s see an example,
Example
For input number 121, Output is “Palindrome number”.
Explanation:
For input num = 121,
After reverse = 121,
Here input num is equal to number after reverse.
Hence we can say that input number 121 is a palindrome number.
Logic to check number is palindrome or not
- Take an input number. Initialize rem (remainder) and rev (reverse) to zero.
- Assign input/original number into num.
- A loop (while or for) iterates as long as number is not zero.
- Inside the loop, the last digit of
number
is extracted using the modulo operator (rem =number%10). - The extracted digit is appended to the rev variable, effectively building the reversed number (rev= (rev*10)+rem).
- Now, the original number is modified to remove the last digit by integer division (number = number/10).
- After completing the loop, the program checks if the original number (num) is equal to the reversed number (rev), if equal, then says palindrome, otherwise, says not a palindrome number using if-else.
Java program to check number is palindrome or not
Program 1: Using while loop
In the below program we are using while loop to check whether a number is palindrome or not in java.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a Integer number");
int number = s.nextInt();
int rem = 0, rev = 0;
int num = number;
while (number !=0) {
rem = number % 10;
rev = (rev * 10) + rem;
number = number / 10;
}
if (num == rev) {
System.out.println(num + " is palindrome");
} else {
System.out.println(num + " is not palindrome");
}
s.close();
}
}
Output:
Enter a Integer number
1221
1221 is palindrome
Java program explanation to check number is palindrome or not
- The above java program asks the user to enter a number.
- Reads the input number using a Scanner object.
- It initializes variables
rem
(remainder),rev
(reverse), andnum
to 0, 0, and the input number, respectively. - Then, it enters a while loop, where the loop continues as long as
number
is not equal to 0. - Inside the loop, it extracts the last digit of the number using the modulus operator
%
, stores it inrem
, and updates the reversed numberrev
by multiplying it by 10 and adding the current digit. - It divides the number by 10 to remove the last digit.
- After the loop completes, it checks if the original number (
num
) is equal to the reversed number (rev
). - If they are equal, it prints that the number is a palindrome. Otherwise, it prints that the number is not a palindrome.
- Finally, it closes the Scanner object.
Program 2: Using for loop
In the below program we are using for loop to check whether a number is palindrome or not in Java.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a Integer number");
int number = s.nextInt();
int rem = 0, rev = 0;
for (int num=number;num !=0;) {
rem = num % 10;
rev = (rev * 10) + rem;
num = num / 10;
}
if (number == rev) {
System.out.println(number + " is palindrome");
} else {
System.out.println(number + " is not palindrome");
}
s.close();
}
}
Output:
Enter a Integer number
456
456 is not palindrome
Java program explanation to check number is palindrome or not
- The java program asks the user to enter a number.
- Reads the input number using a Scanner object.
- Then, it initializes variables
rem
(remainder) andrev
(reverse) to 0. - Now, enters a while loop, where the loop continues as long as
num
is not equal to 0. - Inside the loop, it extracts the last digit of the number using the modulus operator
%
, stores it inrem
, and updates the reversed numberrev
by multiplying it by 10 and adding the current digit. - It divides the number by 10 to remove the last digit.
- After the loop completes, it checks if the original number is equal to the reversed number.
- If they are equal, it prints that the number is a palindrome. Otherwise, it prints that the number is not a palindrome.
- Finally, it closes the Scanner object.
Conclusion
In this tutorial, we have learnt writing the java program to check if a number is palindrome or not. We understood the logic and we have seen the program using for loop in java and while loop in java. Hope this tutorial helped you in the understanding of the program.
You can also check our other awesome tutorials
- Program to print sum of all even numbers in a range in Java
- Check triangle is equilateral, isosceles or scalene in Java
- Java program to check perfect number
- Program to print sum of all odd numbers in a range in Java
Happy Java Programming!!