xxxxxxxxxx
// Recursive Java program to
// check if the number is
// palindrome or not
import java.io.*;
class GFG
{
// recursive function that
// returns the reverse of digits
static int rev(int n, int temp)
{
// base case
if (n == 0)
return temp;
// stores the reverse
// of a number
temp = (temp * 10) + (n % 10);
return rev(n / 10, temp);
}
// Driver Code
public static void main (String[] args)
{
int n = 121;
int temp = rev(n, 0);
if (temp == n)
System.out.println("yes");
else
System.out.println("no" );
}
}
// This code is contributed by anuj_67.
xxxxxxxxxx
// A recursive JAVA program to
// check whether a given String
// is palindrome or not
import java.io.*;
class GFG
{
// A recursive function that
// check a str(s..e) is
// palindrome or not.
static boolean isPalRec(String str,
int s, int e)
{
// If there is only one character
if (s == e)
return true;
// If first and last
// characters do not match
if ((str.charAt(s)) != (str.charAt(e)))
return false;
// If there are more than
// two characters, check if
// middle substring is also
// palindrome or not.
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
static boolean isPalindrome(String str)
{
int n = str.length();
// An empty string is
// considered as palindrome
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
// Driver Code
public static void main(String args[])
{
String str = "geeg";
if (isPalindrome(str))
System.out.println("Yes");
else
System.out.println("No");
}
}