Skip to content
Related Articles
Recursive program to check if number is palindrome or not
• Difficulty Level : Easy
• Last Updated : 22 Mar, 2021

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++

 `// Recursive C++ program to check if the``// number is palindrome or not``#include ``using` `namespace` `std;` `// recursive function that returns the reverse of digits``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``int` `main()``{` `    ``int` `n = 121;``    ` `    ``int` `temp = rev(n, 0);``  ` `    ``if` `(temp == n)``        ``cout << ``"yes"` `<< endl;``    ``else``        ``cout << ``"no"` `<< endl;``    ``return` `0;``}`

## Java

 `// 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.`

## Python3

 `# Recursive Python3 program to check``# if the number is palindrome or not` `# Recursive function that returns``# the reverse of digits``def` `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):``    ``print``(``"yes"``);``else``:``    ``print``(``"no"``);` `# This code is contributed``# by mits`

## C#

 `// Recursive C# program to``// check if the number is``// palindrome or not``using` `System;` `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 ()``{``    ``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

 ``

## Javascript

 ``
Output:
`yes`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up