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 loginOutput
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

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.

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.

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.
