  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# CPP Program to Check whether a Number is Palindrome or Not

Last Updated on May 5, 2023 by Prepbytes A palindrome is a unique number that is exactly the same as from the forward and backward.
We notice palindromes everywhere in daily life, from the numbers on a date like 22-02-2022 to zip codes like 10301. Palindromic numbers can be utilized to address a variety of mathematical conundrums and issues pertaining to computer technology.

## What is Palindrome?

A palindrome is a number that does not change when it is turned around. Examples of palindrome numbers are 121, 34543, 343, 131, and 48984.

When the final digit of a number is the same as the first digit, the second digit is the same as the second-last digit, and so on, a number is said to be palindrome if it reads the same forward as it does backward. In summary, we may say that a number is a palindrome if the result obtained by reversing the digits of the number (reversing the digits’ order) is the same as the result obtained by using the original number.

### Algorithm for Palindrome Number

Let’s now examine the standard procedure for determining if a number is a palindrome or not:

• Step 1: Take a user-provided number N and determine if it is a palindrome or not.
• Step 2: Create a copy of the user-provided number and assigned it to a temporary variable (temp = N)
• Step 3: Create two new variables, digit and reverse, and initialize them to record the number’s digits and the input number’s reverse, respectively.
• Step 4: Initialize a while loop
• Step 5: Utilise the modulus operator to remove the last digit from the given integer(N%10).
• Step 6: Once the past value of the reverse variable has been multiplied by 10, add this digit to the reverse variable. the reverse is equal to the backward multiplied by ten plus the digit.(reverse = reverse * 10 + digit)
• Step 7: The variable n should be updated with a decrement statement so that the following digit from the left side may be retrieved. (n = n / 10)
• Step 8: Follow steps 5 to step7 until the N is more than zero(N>0).
• Step 9: End

### Dry Run of Palindrome Number in C++

To comprehend how the iterative approach for testing palindrome in C++ works, let’s track the values of these three variables for the input number 1991 throughout each iteration of the while loop.

Initially:

• N = 1991
• Reverse = 0
• temp = 1991

Repetition 1:

• N = 199
• Reverse = 1
• temp = 1991

Repetition 2:

• N = 19
• Reverse = 19
• temp = 1991

Repetition 3:

• N = 1
• Reverse = 199
• temp = 1991

Repetition 4:

• N = 0
• Reverse = 1991
• temp = 1991

Here, we can see that a digit from the input number is subtracted and stored in the reverse variable after each iteration. Once all of the digits have been saved in the reverse variable in reverse order, the loop ends. To determine if the input number is a palindrome or not, the reversed number is compared to the number kept in the temporary variable.

As a result, after applying the aforementioned procedure, the reverse variable will retain the input number’s inverse, and the temp variable will hold the input number’s original value. Let’s see how this iterative approach to palindrome number verification may be applied in C++.

### Code Implementation

```#include <bits/stdc++.h>
using namespace std;

int main()
{
int N=1991, digit, reverse = 0, temp;

temp = N;

//loop to find reverse number
while(temp != 0)
{
digit = temp % 10;
reverse = reverse * 10 + digit;
temp /= 10;
};

if (N == reverse)
cout << N << " is Palindrome";
else
cout << N << " is not a Palindrome";

}
```

Output:

``1991 is Palindrome``

The input number 1991 is used to determine if the number is a palindrome or not. The program checks a number’s palindrome using the iterative C++ approach. Here, we can see that when the number 1991 is reversed, it produces the number 1991, which is the same as the original number. As can be seen from the output of the program, the number 1991 is Palindrome.

There is one more method to check number is palindrome or not by Mathematical technique using recursion

### Mathematical Technique using Recursion

We can also check if the number is palindrome or not using the recursive method.
In this, we can see that a digit from the input number is subtracted and stored in the reverse variable after each call. Once all of the digits have been saved in the reverse variable in reverse order, the loop ends. To determine if the input number is a palindrome or not, the reversed number is compared to the number kept in the temporary variable.

Code Implementation:

```#include<iostream>
using namespace std;

int ReverseNumber(int N, int rev){
if(N == 0)
return rev;

int rem = N % 10;
rev = rev * 10 + rem;

return ReverseNumber(N / 10, rev);
}

int main ()
{
int N, reverse = 0;
N=1991;

if (ReverseNumber(N, reverse) == N)
cout << N << " is Palindrome";
else
cout << N << " is not a Palindrome";

}```

Output

``1991 is Palindrome``

Conclusion
Any number that remains intact when reversed, i.e., reads the same forward as it does backward, is known as a palindrome and you must first determine a number’s reverse before determining if it is a palindrome and using either the Iterative technique or User-defined methods, the C++ palindrome program is put into action.

Q1. How can you determine using arithmetic whether a number is a palindrome?
Ans. A number is said to be a palindrome if its own value is the same as its opposite, for example, 313 is a palindrome since its opposite is likewise 313. However, 123 is not a palindrome because its reverse, 321, is not the same as 123, the original number.

Q2. What are the first 10 palindrome numbers?
Ans. Therefore, the first few palindromic numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121.

Q3. What does the C++ recursion formula mean?
Ans. Recursion is a C++ procedure that repeatedly calls itself until a predetermined condition is satisfied. This technique has a base case and a recursive condition, and it continuously calls the same function.

Q4. Can a C++ function return two values?
Ans. Multiple values cannot be directly returned from a function in C or C++.

Q5. Can C++ return more than one type?
Ans. The std::pair, std::tuple, or a local struct can be used to return multiple values from a function even though there is no official mechanism to do so in C++.