In this tutorial you will learn about the Java Program to check Palindrome string using Recursion and its application with practical example.
Java Program to check Palindrome string using Recursion
In this tutorial, we will learn to create a Java program that will check Palindrome string using Recursion using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Conditional Statements in Java programming.
- Arithmetic operations in Java Programming.
What is a palindrome string/number?
The Palindrome string/number means a string/number that is the same when we read it from forward and reverse.
For Example 1:- AddA is a palindrome string that is the same when we write it in reverse order, 1221
Example 2:- 1221 this is a palindrome number that is the same when we write it in reverse order, 1221.
Example 3:- 123 this is not a palindrome number, it is not the same when we write it in reverse order, 321.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Calculating the string is a Palindrome string or not. 4. Printing the result. 5. End the program. |
Program to Check Whether a String is Palindrome or Not:-
To check whether a number is a Palindrome string or not. In today’s Java program, we will write code for checking. First, we will take a string of input from the user. Then we will pass it into the program to Check Whether a String is Palindrome or Not.
With the help of the below program, we can Check the Palindrome string or Not.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Scanner; public class RecPalJava { // to check if string is palindrome using recursion public static boolean checkPalindrome(String str) { //checking the Palindrome string if(str.length() == 0 || str.length() == 1) return true; if(str.charAt(0) == str.charAt(str.length() - 1)) return checkPalindrome(str.substring(1, str.length() - 1)); return false; } //Body of the main function public static void main(String[]args) { Scanner sc = new Scanner(System.in); //Taking input from the user for the program. System.out.println("Please enter a string : "); //Declaring the required variable for the program. String strg = sc.nextLine(); //Printing the output of the program palindriome or not. if(checkPalindrome(strg)) { System.out.println(strg + " is palindrome"); } else { System.out.println(strg + " not a palindrome"); } sc.close(); } } |
Output:-
In the above program, we have first initialized the required variable.
- strg = it will hold the string value of the input for the program.
Input message for the user for the string value.
Program Logic Code to check the string is palindrome or not.
Printing output Palindrome number or not.