Recursive program to check if number is palindrome or not - GeeksforGeeks

24 Feb, 2022
Given a number, the task is to write a recursive function which checks if the given number is palindrome or not. Examples:  
Input : 121
Output : yes

Input : 532
Output : no
 
The approach for writing the function is to call the function recursively till the number is completely traversed from the back. Use a temp variable to store the reverse of the number according to the formula which has been obtained in this post. Pass the temp variable in the parameter and once the base case of n==0 is achieved, return temp which stores the reverse of a number. Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// Recursive C++ program to check if the// number is palindrome or not#include <bits/stdc++.h>using namespace std; // recursive function that returns the reverse of digitsint 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 Codeint main(){     int n = 121;         int temp = rev(n, 0);       if (temp == n)        cout << "yes" << endl;    else        cout << "no" << endl;    return 0;}
// Recursive Java program to// check if the number is// palindrome or notimport java.io.*; class GFG{ // recursive function that// returns the reverse of digitsstatic 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 Codepublic 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.
# Recursive Python3 program to check# if the number is palindrome or not # Recursive function that returns# the reverse of digitsdef rev(n, 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 Coden = 121;temp = rev(n, 0); if (temp == n):    print("yes")else:    print("no") # This code is contributed# by mits
// Recursive C# program to// check if the number is// palindrome or notusing System; class GFG{ // recursive function// that returns the// reverse of digitsstatic 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 Codepublic static void Main (){    int n = 121;    int temp = rev(n, 0);         if (temp == n)        Console.WriteLine("yes");    else        Console.WriteLine("no" );}} // This code is contributed// by anuj_67.
<?php// Recursive PHP program to check// if the number is palindrome or not // Recursive function that returns// the reverse of digitsfunction rev($n, $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$n = 121;$temp = rev($n, 0); if ($temp != $n)    echo "yes";else    echo "no"; // This code is contributed// by Sach_Code?>
<script> // Recursive Javascript program to check if the// number is palindrome or not // recursive function that returns the reverse of digitsfunction rev(n, temp){    // base case    if (n == 0)        return temp;     // stores the reverse of a number    temp = (temp * 10) + (n % 10);     return rev(Math.floor(n / 10), temp);} // Driver Code     let n = 121;         let temp = rev(n, 0);         if (temp == n)        document.write("yes" + "<br>");    else        document.write("no" + "<br>"); // This code is contributed by Mayank Tyagi </script>
yes
 
vt_m
Sach_Code
Mithun Kumar
mayanktyagi1709
amartyaghoshgfg
number-digits
palindrome
Mathematical
Mathematical
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Program for Decimal to Binary Conversion