Palindrome In Java(String & Number) – Check 2 Easy Methods

A String or Number is called a Palindrome, when reversed String will be the same as the given String. In this article, we will see how to check a given String or Number is a palindrome in java or not.

Example of palindrome:

Palindrome in java

Some other palindrome Strings are level, refer, wow, mom, dad, mam, and more.

Some palindrome numbers are 858, 96569, 14241, 252, and more.

We can check Palindrome for both String and Number in Java. We can check the same program of String to check Palindrome. In this case, when we will check for any number that number will be treated as a string literal which is not a good standard of programming. So we have taken two different examples of programs to check Palindrome in Java for both String and Number.

In the first program, we will check for any String input and in the second program, we will check for any Number input.

Let’s see both programs.

Program 1: String program to check palindrome using in Java

In the String Palindrome program, we can use logic to reverse a String, and then we will check the reversed String is similar to the given String or not. If reversed String will be equal to given String, then given String will be Palindrome, otherwise not.

Algorithm to check palindrome in Java:

Declare a String str = "Codingface" Display given String. Declare an empty String revStr = "" Iterate String using for loop for(int pos = lastIndex - 1; pos >= 0; pos++) Write logic for reverse string (Reverse String = Reverse String + str.charAt(pos)) Display reversed String Check whether reversed String is equal to the given String or not. If it is equal, print Given String is Palindrome, otherwise, Given String is not a Palindrome.
Code language: JavaScript (javascript)

Java program to check string palindrome:

package cf.java.string; public class PallindromeInJava { public static void main(String[] args) { // Declare a String with default value "Codingface" String str = "refer"; // Print given String System.out.println("Input String: " + str); // Declare a new empty String to store reversed String String revStr = ""; // Use a for loop to start iteration from last character to first character for (int i = str.length() - 1; i >= 0; i--) { revStr += str.charAt(i); } // Print String after reverse System.out.println("String after reverse: " + revStr); // Check whether reversed string is same to given string or not if (revStr.equals(str)) System.out.println("Yes, Given String is a Pallindrome"); else System.out.println("No, Given String is not a Pallindrome"); } }
Code language: Java (java)

Output:

Input String: refer String after reverse: refer Yes, Given String is a Palindrome
Code language: JavaScript (javascript)

Explanation of above program:

In the above program, we have used simply for loop to reverse String, and then we have checked whether a given string is palindrome or not. 

Similarly, you can also use other methods to check a palindrome in Java-like while loop, recursion, reverse( ) method, and toCharArray( ) method

You can check the below post to reverse a String in java and then you can check reversed String is a palindrome or not by using lines no 20 to 23 of the above program.

Also Read: Check how to reverse  a String in Java – 5 methods

Program 2: Number Program To Check Palindrome In Java

In number palindrome program, we can use logic to reverse a number and then we will check the reversed number is similar to given number or not. If reversed number will be equal to given number, then given number will be a palindrome number, otherwise not.

Algorithm to check palindrome of a number:

Accept any number from the user. Store given number in temp to check later. Use while loop until 'num > 0' Use logic to reverse a number Check whether the reversed number is equal to the given number or not. If it is equal, print given number is a Palindrome Number, otherwise print given number is not a Palindrome Number.
Code language: JavaScript (javascript)

Java program to check number palindrome:

package cf.java.num; import java.util.Scanner; public class NumberPallindromeInJava { public static void main(String[] args) { // Declare number, reverse, reminder & temporary int num, rev = 0, rmd = 0, temp = 0; // Used Scanner class to recieve any number from User Scanner sc = new Scanner(System.in); System.out.print("Enter A Number: "); num = sc.nextInt(); // Store given number in temp variable temp = num; // Use while loop to reverse given number while (num > 0) { rmd = num % 10; rev = (rev * 10) + rmd; num = num / 10; } // Check whether given number is a palindrome or not if (rev == temp) { System.out.println(temp + " Is A Palindrome Number"); } else { System.out.println(temp + " Is Not A Palindrome Number"); } } }
Code language: Java (java)

Output:

Enter A Number: 96569 96569 Is A Palindrome Number
Code language: JavaScript (javascript)

People also ask for:

  1. How to write a Palindrome program in Java?
  2. Example of program for Palindrome in Java.
  3. How to check Palindrome in Java program.
  4. How to check a Palindrome Number in Java.

3 thoughts on “Palindrome In Java(String & Number) – Check 2 Easy Methods”

Leave a Comment