-0.8 C
New York
Wednesday, February 21, 2024

# Java Program for Palindrome

What is Palindrome Number and String?

A palindromic number is a kind of number which remains the same even after its digit is reversed for example 121, 1551 etc are palindrome numbers because they remain the same even after you reverse their digit.

Same in the case of palindrome string, a string is said to be a palindromic string when even after reversing the character of string the original string remains same, for example, DAD is a palindromic string.

In this tutorial, you will learn how to write a java program to check if an integer or a string is a palindrome or not.

### 1) Check Palindrome Integer in Java Using While Loop

``````//Java Program Example for checking number is palindrome or not using while loop

//Import Scanner to take input from user
import java.util.Scanner;

//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter an integer: ");
//Taking entered number in variable of int type
int num = scn.nextInt();
//Declaring and Initilizing variables
int digit,digitSum=0,temp;
//Initilizing temp variable with the entered number
temp = num;

//Checking weather temp variable greater than 0
while(temp > 0)
{
//If Condiation is true

//Applying Modulation on temp variable
digit = temp % 10;
//Applying the formula to get the sum
digitSum = (digitSum * 10) + digit;
//Removing last digit from the number
temp = temp/10;
}

//Checking if entered number is equal to the formula sum
if(digitSum == num) {
//If Yes, Then it is a palindrom number
System.out.println(num+" IS A PALINDROME NUMBER");
}else{
//If No, Then it is not a palindrom number
System.out.println(num+" IS A NOT PALINDROME NUMBER");
}
}
}
``````

OUTPUT :

``````1) Enter an integer: 121
121 IS A PALINDROME NUMBER

2) Enter an integer: 199
199 IS A NOT PALINDROME NUMBER``````

### 2) Check Palindrome Integer in Java Using For Loop

``````//Java Program to check whether the entered number is palindrome or not using for loop

//Import Scanner to take input from user
import java.util.Scanner;

//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter an integer: ");
//Taking entered number in variable of int type
int num = scn.nextInt();
//Declaring and Initilizing variables
int digit,digitSum,temp;
//Initilizing temp variable with the entered number
temp = num;

//For loop for the iteration till temp number is greater than 0
for(digitSum = 0; temp > 0; temp /= 10){
//Applying Modulation on temp variable
digit = temp % 10;
//Applying the formula to get the sum
digitSum = (digitSum * 10) + digit;
}
//Checking if entered number is equal to the formula sum
if(digitSum == num) {
//If Yes, Then it is a palindrom number
System.out.println(num+" IS A PALINDROME NUMBER");
}else{
//If No, Then it is not a palindrom number
System.out.println(num+" IS A NOT PALINDROME NUMBER");
}
}
}``````

OUTPUT:

``````1) Enter an integer: 121
121 IS A PALINDROME NUMBER

2) Enter an integer: 123
123 IS A NOT PALINDROME NUMBER``````

## String Palindrome in Java

### 3) Check Palindrome String in Java Using For Loop

``````//Java Program Example to String palindrome using for loop

//Import Scanner to take input from user
import java.util.Scanner;

//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of int type
String str = scn.nextLine();
//Declaring and Initilizing a variable of String Type
String revStr="";
//Declaring a variable of Integer Type
int i;
//Get the length of string
int strLen = str.length();
//For loop for iteration
//Iteration will go on till the string length variable greater than equal to 0
for(i = strLen - 1; i >= 0; i--){
//Adding the reverse string and charecter at of string at `i` position
revStr = revStr + str.charAt(i);
}
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
} ``````

OUTPUT :

``````1) Enter a string: dad

2) Enter a string: Java
Java IS A NOT PALINDROME STRING``````

### 4) Check Palindrome String in Java Using While Loop

``````//Java Program Example to String palindrome using while loop

//Import Scanner to take input from user
import java.util.Scanner;

//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of string type
String str = scn.nextLine();
//Declaring and Initilizing a variable of String Type
String revStr="";
//Declaring a variable of Integer Type
int i;
//Get the length of string
int strLen = str.length();
//Initilizing our local integer variable with the length of string - 1
//Because we are checking minimum lenght equals to 0
i = strLen - 1;
//While Loop iteration till sting length variable greater than equals to 0
while(i >= 0){
//Adding the reverse string and charecter at of string at `i` position
revStr = revStr + str.charAt(i);
//Redusing the i value by 1
i--;
}
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
} ``````

OUTPUT:

``````1) Enter a string: dad

2) Enter a string: core java
core java IS A NOT PALINDROME STRING``````

### 5) Check Palindrome Using Java Library Functions

``````//Java Program Example to check palindrome string using Library Function

//Import Scanner to take input from user
import java.util.Scanner;

// Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of String type
String str = scn.nextLine();
//Reversing the string with the help of Library Function
String revStr = new StringBuffer(str).reverse().toString();

//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
}``````

OUTPUT:

``````1) Enter a string: dad

2) Enter a string: String
String IS A NOT PALINDROME STRING
``````