Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Program to Check Palindrome
In this article, we will learn multiple ways to check whether a number is palindrome or not in Java.
Palindrome Number:
A number is said to be a palindrome number if reverse of a number is same as that of the original number.
E.g. 131, 9865689, 1234321, 555 are palindrome numbers
And our task is to find out different approaches to check whether a number is Palindrome or not. We will see to implement this.
Let’s see different approaches one by one.
- Check a number is Palindrome or not using While Loop
- Check a number is Palindrome or not using For Loop
- Check a number is Palindrome or not by taking user input
- Check a number is palindrome or not using Static method
- Check a number is Palindrome or not using Recursion
Method-I:- Check a number is Palindrome or not using While Loop
Using a while loop a number can be checked if it is palindrome or not. Initially we store the number in a variable say orgno. Then by taking a while loop we will calculate reverse of a number. Then we will check if that variable orgno is equal to the number calculated in while loop. If condition satisfied, print the number is palindrome. Otherwise, the number is not palindrome.
Approach:
- Declare and initialize int variable
no(number to checked) andrevnoto 0. - Declare int variable
remto contain remainder. - Initially store
noin a variable sayorgno - Take a while loop and iterate until
no!=0 - Inside while loop implement the following
- Store
no%10inrem - Implement
revno * 10 + remand store inrevno - Make
no/10and store inno - After the while loop condition fails,
revnowill contain reverse ofno - Taking if condition check if
orgno=revno, if yes it is palindrome. - Otherwise, it is not palindrome.
Program:
class CheckPalindome
{
public static void main(String[] args)
{
int no = 12421, revno = 0, rem;
// store the number in orgnum
int orgno = no;
// get the reverse of no and store in revno
while (no != 0) {
rem = no % 10;
revno = revno * 10 + rem;
no /= 10;
}
// check if revno and orgno i.e. no are equal
if (orgno == revno) {
System.out.println(orgno + " is a Palindrome number.");
}
else {
System.out.println(orgno + " is not a Palindrome number.");
}
}
}
Output: 12421 is a Palindrome number.
Method-II:- Check a number is Palindrome or not using For Loop
We can check a number is palindrome or not using a for loop. Initially we store the number in an integer variable say orgno. Then by taking a for loop we will calculate reverse of a number. Then we will check if that variable orgno is equal to the number calculated in for loop. If condition satisfied, print the number is palindrome. Otherwise, the number is not palindrome.
Approach:
- Declare and initialize int variable
no(number to checked) andrevnoto 0. - Declare int variable
remwhich will contain remainder. - Initially store no in a variable say
orgno - Take a for loop iterate till
no!=0then makeno/10 - Store
no%10inrem - Implement
revno * 10 + remand store inrevno - After the for loop condition fails
revnowill contain reverse ofno - Taking if condition check if
orgno=revno, if yes it is palindrome. - Otherwise, it is not palindrome.
Program:
public class CheckPalindrome
{
public static void main(String[] args)
{
int no=423321, revno=0, rem, orgno;
// store the value of no in orgno
orgno = no;
// on each iteration no will be divided by 10
for( ;no != 0; no /= 10 )
{
// store the remainder no/10 in rem
rem = no % 10;
// reversed number will be stored in revno
revno= revno* 10 + rem;
}
// check if original no is equal to reverse no
if (orgno== revno)
System.out.println(orgno + " is a palindrome number.");
else
System.out.println(orgno + " is not a palindrome number.");
}
}
Output: 423321 is not a palindrome number.
Method-III:- Check a number is Palindrome or not by taking user input
This method is almost similar to Method-I. But we only have to take the number as user input. Then proceed similarly as approach of Method-I.
Approach:
- Declare and initialize int variable
revnoto 0. - Declare int variable
nowhich will store number to be checked. - Take a number input from user.
- Declare int variable
remto contain remainder. - Initially store
noin a variable sayorgno - Take a while loop and iterate until
no!=0 - Inside while loop implement the following
- Store
no%10inrem - Implement
revno * 10 + remand store inrevno - Make
no/10and store inno - After the while loop condition fails,
revnowill contain reverse ofno - Taking if condition check if
orgno=revno, if yes it is palindrome. - Otherwise, it is not palindrome.
Program:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int no, revno = 0, rem;
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number : ");
no= sc.nextInt();
// store the number in 'orgno'
int orgno = no;
// get the reverse of 'no' and store in 'revno'
while (no != 0) {
rem = no % 10;
revno = revno * 10 + rem;
no /= 10;
}
// check if 'revno' and 'orgno' i.e. 'no' are equal
if (orgno == revno)
{
System.out.println(orgno + " is a Palindrome number.");
}
else
{
System.out.println(orgno + " is not a Palindrome number.");
}
}
}
Output: CASE-1 Enter a number : 131 131 is a Palindrome number. CASE-2 Enter a number : 654 654 is not a Palindrome number.
Method-IV :- Check a number is palindrome or not using Static method
Static method is that method which belongs to class but not to the object. It will exist whatever may be the instance created for the class.
Approach:
- Declare integer variable say
revno,orgno. - Declare and initialize the number to be checked say int variable
no. - Store the value of
noinorgno. - Take a static method and pass no’s value in it.
- In static method implement the following
- Declare integer variable
sum,numand initializesumto 0. - Iterate while loop until passed value say
n(given number) !=0. - Inside while implement the following.
- Store
n%10in integer variablerem - And store
(sum*10)+reminsum. - Then do
n=n/10. - After while loop executed come out of the loop and store the sum value to the calling method in main function.
- Check whether
returned value =orgno, print number is palindrome, otherwise not palindrome.
public class Main
{
public static void main(String arg[])
{
int revno;
int no=12521;
int orgno=no;
// reverse no will be stored in revno
revno=palindromeOrNot(no);
// checks reverse number=original number
if(revno==orgno)
System.out.println(revno+" is a palindrome number ");
else
System.out.println(revno+" is not a palindrome number ");
}
// static method to compute reverse no and return to main method
static int palindromeOrNot(int n)
{
int sum=0,rem;
// get reverse of number
while(n!=0)
{
rem=n%10;
sum=(sum*10)+rem;
n/=10;
}
// return reverse number to calling method
return sum;
}
}
Output: 12521 is a palindrome number
Method-V :- Check a number is Palindrome or not using Recursion
Here a static method will be as recursive function. It will execute recursively and return the reversed number. If it is equal to the original number then the number is palindrome, otherwise not.
Approach
- Declare and initialize integer variable say
nowhich is the number to be checked. - Initialize integer variable say
revno - Take static method say
revRecursion - Create static method which will accept arguments passed in main function i.e.
static int revRecursion(int no, int revno) - Since the method will execute recursively, so execute it until
nobecomes 0. - Implement
(revno * 10) + (no % 10)and store inrevno. After some executions returnno/10andrevno. - In main function the returned value will be stored in
revno. - Check if
revno=no, it is a palindrome number. - otherwise it is not a palindrome.
Program:
public class CheckPalindrome
{
// recursive method will execute until no becomes 0
static int revRecursion(int no, int revno)
{
// if no becomes 0 return revno
if (no == 0)
return revno;
// reverse of no stored in revno
revno = (revno * 10) + (no % 10);
// no will be divided by 10
// will again call recursive method
return revRecursion(no / 10, revno);
}
// Driver Code
public static void main (String[] args)
{
int no = 9658569;
// store the returned value from recursion method in revno
int revno = revRecursion(no, 0);
// check if reversed number = original number
if (revno == no)
System.out.println(no+" is a Palindrome number.");
else
System.out.println(no+" is a Palindrome number" );
}
}
Output: 9658569 is a Palindrome number.
Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.
Related Java Decision Making and Loop Programs:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)