Table of Contents
Example
The method used in the program below is as follows
Output
Home Backend Development C++ Recursive program to check if a number is a palindrome in C++

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

Sep 10, 2023 pm 01:25 PM
number recursion palindrome number

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recursive implementation of C++ functions: Is there a limit to recursion depth? Recursive implementation of C++ functions: Is there a limit to recursion depth? Apr 23, 2024 am 09:30 AM

The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

A beginner's guide to C++ recursion: Building foundations and developing intuition A beginner's guide to C++ recursion: Building foundations and developing intuition May 01, 2024 pm 05:36 PM

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.

C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application Apr 30, 2024 am 10:45 AM

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

C++ function recursion explained: alternatives to recursion C++ function recursion explained: alternatives to recursion May 01, 2024 pm 04:54 PM

Recursion is a technique where a function calls itself, but has the disadvantages of stack overflow and inefficiency. Alternatives include: tail-recursion optimization, where the compiler optimizes recursive calls into loops; iteration, which uses loops instead of recursion; and coroutines, which allow execution to be paused and resumed, simulating recursive behavior.

Detailed explanation of C++ function recursion: tail recursion optimization Detailed explanation of C++ function recursion: tail recursion optimization May 03, 2024 pm 04:42 PM

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.

See all articles