What is Palindrome number?
A number is said to be a palindrome if the number is same after reverse. For example - 11, 121, 999, 45654.
What is Palindrome String?
A String is said to be a palindrome if the String is same after reverse. For example - MOM, MADAM, LEVEL, MALAYALAM.
There are many ways to implement the Palindrome program in Java. But today we will discuss some of their important ways with example.
So let's Start..
Read Also : Check Whether a Number is Prime or not.
Palindrome number program in java.
String palindrome program in java.
Check palindrome number in java using for loop.
Algorithm:
class Palindrome
{
public static void main(String args[])
{
int remainder;
int sum=0;
int number = 12521;//This is the number that needs to be checked
int temp = number; // holding input number in temp variable.
for( ; number!=0;number/=10 ) //number/=10, means number=number/10;
{
remainder = number%10; //getting remainder.
sum = (sum*10)+remainder; // getting reverse.
}
if(temp == sum) //Comparing original number(temp) with reversed number(sum).
System.out.println(temp+" is Palindrome Number.");
else
System.out.println(temp+" is Not Palindrome Number.");
}
}
Check whether input number is Palindrome or not using while loop in java.
Algorithm:
import java.util.*;
class Palindrome{
public static void main(String args[]){
int r,sum=0;
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter the number");
int number = scan.nextInt(); // taking input from user.
int temp = number; // holding input number in temp variable.
while(number>0){
r = number%10; //getting remainder.
sum = (sum*10)+r; // getting reverse.
number = number/10;
}
if(temp == sum) //Comparing original number(temp) with reversed number(sum).
System.out.println(temp+" is Palindrome Number.");
else
System.out.println(temp+" is Not Palindrome Number.");
}
}
Palindrome string in java.
In this example we will check given string is palindrome or not using array.
Example -1
Algorithm:
import java.util.Arrays;
import java.util.Scanner;
class Palindrome {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter a string:");
String str = scan.nextLine();
char[] chArray = str.toCharArray();//toCharArray() method is used to convert string to character array.
int size = chArray.length;
char [] originalArray = Arrays.copyOf(chArray,size);//Arrays.copyOf() method is used to copy the array.
for (int i = 0; i < size / 2; i++)
{
char temp = chArray[i];
chArray[i] = chArray[size-i-1];
chArray[size-i-1] = temp;
}
System.out.println("Original Array = "+Arrays.toString(originalArray));
System.out.println("Reverse Array = "+Arrays.toString(chArray));
if(Arrays.equals(chArray, originalArray))//Arrays.equals() method has used to check whether two arrays are equal.
{
System.out.println("Input string is a palindrome");
} else {
System.out.println("Input string is not a palindrome");
}
}
}
Check given String is palindrome or not in java using array(Another way).
Example -2
Algorithm:
import java.util.Arrays;
import java.util.Scanner;
class Palindrome {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter a string:");
String str = scan.nextLine();
char[] chArray = str.toCharArray();//toCharArray() method is used to convert string to character array.
int size = chArray.length;
char [] originalArray = Arrays.copyOf(chArray,size);//Arrays.copyOf() method is used to copy the array.
for (int i = 0; i < size / 2; i++)
{
char temp = chArray[i];
chArray[i] = chArray[size-i-1];
chArray[size-i-1] = temp;
}
System.out.println("Original Array = "+Arrays.toString(originalArray));
System.out.println("Reverse Array = "+Arrays.toString(chArray));
if(Arrays.equals(chArray, originalArray))//Arrays.equals() method has used to check whether two arrays are equal.
{
System.out.println("Input string is a palindrome");
} else {
System.out.println("Input string is not a palindrome");
}
}
}
Check if a string is palindrome or not in java using reverse method.
Algorithm:
import java.util.*;
class Palindrome{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome:");
String input = scan.nextLine();
String reverse = new StringBuffer(input).reverse().toString();//The reverse() method reverses an array in place
if (input.equals(reverse))
{
System.out.println("Yes, it is a palindrome");
}else{
System.out.println("No, it is not a palindrome");
}
}
}
This is all about palindrome, in this article we have discussed what is palindrome number, what is palindrome string, and we also have discussed the code that, how to check palindrome number in java, how to check whether a string is palindrome or not with meaningful example. We hope you understand well. If you have any doubts, please comment in the comment box.
0 Comments