Home > Backend Development > C++ > body text

Recursive program to check if a number is a palindrome in C++

WBOY
Release: 2023-09-10 13:25:09
forward
1242 people have browsed it

Recursive program to check if a number is a palindrome in C++

We get an integer as input. The goal is to use recursion to determine whether the input number Num is a palindrome.

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

Example

Input− Num = 34212;

Output− 34212 is not a palindrome!

Explanation− If we reverse 34212, we get 21243. 34212 != 21243 Therefore the entered number is not a palindrome.

Input− Num = 32123;

Output− 32123 is a palindrome!

Explanation - If we reverse 32123, we get 32132. 32123!= 32123, so the input number is a palindrome.

The method used in the program below is as follows

In this method, we use the recursive function revrsNum(int num1, int num2), which accepts the input number num1 and the temporary number num2.

For base case -: If num1 is 0, return num2.

p>

Else-: Use recursion to calculate the reverse order of num1. Returns the reciprocal of the calculation.

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

  • Get the input number Num.

    • Get the input number Num. p>

    • Get Num2 = revrsNum(Num,0)

    • The function revrsNum(int num1, int num2) recursively generates the inverse value of num1, and Returns the reversed number.

    • If num1 is 0, the inverted calculation result returns num2.

    • Otherwise multiply num2 by 10 and add num1 .

    • Use num1=num1/10 to reduce num1 by 10.

    • Use revrsNum(recursive num1, num2);

    • to return the result.

    • Print the results 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;
    }
    Copy after login

    Output

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

    1345431 is Palindrome!
    Copy after login

The above is the detailed content of Recursive program to check if a number is a palindrome in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template