
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Recursive program to check if number is palindrome or not in C++
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.
Examples
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.
Example
#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!"; } else{ cout <<Num<<" is not a Palindrome!"; } return 0; }
Output
If we run the above code it will generate the following Output
1345431 is Palindrome!
- Related Articles
- Write a C# program to check if a number is Palindrome or not
- Check if number is palindrome or not in Octal in Python
- C++ Program to Check Whether a Number is Palindrome or Not
- Python program to check if a string is palindrome or not
- C Program to check if an Array is Palindrome or not
- C# program to check if a string is palindrome or not
- Swift Program to check if an Array is Palindrome or not
- Golang Program to check if the binary representation of a number is palindrome or not
- C Program to check if an array is palindrome or not using Recursion
- Program to check if an Array is Palindrome or not using STL in C++
- Queries to check if substring[L…R] is palindrome or not in C++ Program
- Program to check if an array is sorted or not (Iterative and Recursive) in C
- Program to check a string is palindrome or not in Python
- Recursive function to check if a string is palindrome in C++
- 8085 program to check whether the given 16 bit number is palindrome or not
