Recursive program to check if number is palindrome or not in C++

C++Server Side ProgrammingProgramming

We are given an integer as input. The goal is to find whether the input number Num is a palindrome or not using recursion.

To check if a number is palindrome, reverse that number and check if both numbers are the same or not. If the reversed number is equal to the original number, then it is palindrome.


Input − Num = 34212;

Output − 34212 is not a Palindrome!

Explanation − If we reverse 34212 then we get 21243. 34212 != 21243 hence the input number is not palindrome.

Input − Num = 32123;

Output − 32123 is Palindrome!

Explanation − If we reverse 32123 then we get 32132. 32123!= 32123 hence the input number is palindrome.

Approach used in the below program is as follows

In this approach we are using the recursive function revrsNum(int num1, int num2) which takes input number num1 and temporary number num2.

For base cases-: if num1 is 0 return num2.

Else-: calculate reverse of num1 using recursion. Return calculated reverse.

If both are the same, then the input number is palindrome.

  • Take the input number Num.

  • Take Num2 = revrsNum(Num,0)

  • Function revrsNum(int num1, int num2) generates reverse of num1 recursively and returns the reversed number.

  • If num1 is 0 then return num2 as calculated reverse.

  • Else multiple num2 by 10 and add num1%10 to it.

  • Reduce num1 by 10 using num1=num1/10.

  • Recurse using revrsNum(num1, num2);

  • Return result.

  • Print result obtained inside main.


#include <bits/stdc++.h>
using namespace std;
int revrsNum(int num1, int num2){
   if (num1 == 0){
      return num2;
   num2 *= 10;
   num2 += (num1 % 10);
   num1 = num1/10;
   return revrsNum(num1, num2);
int main(){
   int Num = 1345431;
   int Num2 = revrsNum(Num,0);
   if (Num == Num2){
      cout <<Num<<" is Palindrome!";
      cout <<Num<<" is not a Palindrome!";
   return 0;


If we run the above code it will generate the following Output

1345431 is Palindrome!
Published on 02-Nov-2021 08:28:12