# Check if a number with even number of digits is palindrome or not

The palindrome refers to same number when the digits are reversed. In this program, we need to set the condition and operation based on an even number of digits to identify whether the given input satisfied for palindrome or not. In C++, we have STL(Standard template library) functions such as to_string(), length(), rbegin(), rend(), and, push_back() will be used to Check if a number with an even number of digits is palindrome or not. For example- 2662, 42124, 44, 888, etc are satisfied for even digit palindrome numbers.

## Using for loop and if-else statement

The program uses for loop to set the iteration and condition based on an even number and using an if-else statement it checks the condition whether it’s palindrome or not.

### Syntax

The following syntax is used in the examples-

to_string()


The built-in function to_string() accepts a single integer variable to convert into string.

length()


The length() is a predefined function of C++ that returns the length of the string.

### Algorithm

The following steps are:

Step 1: Start the program by mentioning all the necessary header files such as iostream and string.

Step 2: Then use the function definition named isPalimdrome() with bool type that accepts parameter as integer variable n to work on even palindrome number.

Step 3: Next, using the built-in function to_string it converts an integer into string type and store it in the variable int_str. Then calculate the length of the variable named int_str and store it in the variable len.

Step 4: Using for loop the variable i iterate through the length of the string and sets the condition for an even palindrome number i.e. len/2 and increment to check the even value ranges.

Step 5: Then using if-statement it checks whether the character at present index i is equivalent to the character at the corresponding index from the end of the string len - i - 1. If all pairs of characters become unequal it returns false and if it is found to be equal then it returns true.

Step 6: Start initializing integer n to set the value and using an if-else statement it accept the parameter as a calling function named isPalimdrome() to check whether it's satisfied for even palindrome or not and print the result.

### Example

In the following example, we will check whether the even number of digits is palindrome or not.

#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(int n) {
// Using predefined function to_string() it converts the integer into string
string int_str = to_string(n);
// Using length() it finds the length of the string
int len = int_str.length();
// Set the condition for even palindrome number
for (int i = 0; i < len / 2; i++) {
if (int_str[i] != int_str[len - i - 1]) {
return false;
}
}
return true;
}
int main() {
// integer set to even number
int num = 23132;
if (isPalindrome(num))
{
cout << "The given number is a satisfied palindrome. " << endl;
}
else
{
cout << "The given number is not satisfied for palindrome." << endl;
}
return 0;
}


### Output

The given number is a satisfied palindrome.


## Using vector library

The program uses vector library to provide some of the relatable pre-defined function such as rbegin(), rend(), and, push_back() to solve the even number of digit is palindrome or not.

### Syntax

The following syntax is used in the example-

rbegin()


The rbegin() is a pre-defined function of C++ STL that point to the end character of the given input string.

rend()


The rend() is a pre-defined function of C++ STL which points to reverse end of the vector. In other term, it points to the position before the first character.

push_back()


The push_back() is a famous pre-defined function that follows the vector library. The function is used to remove the element from the back/end.

### Algorithm

The following steps are:

Step 1: We will start the program by including all the necessary header files.

Step 2: Initialize two vector variables of integer type-

• test(int n) - Receive the integer value from the variable num mentioned in the main function.

• result- This variable is used for the result calculation of the problem statement.

Step 3: Next, using for loop the variable i iterate through the input integer and set the condition using < operator to find all the ranges according to given integer variable n. Then increment with ‘+= 2’ that will check the position on the even number and find the palindrome number.

Step 4: Now there will be used some predefined function such as to_string()- convert the integer into string, rbegin(), and, rend()- check for reversing on both sides and using if-statement to check whether its palindrome number or not.

Step 5: To start the main function, initialize the variable n to set the integer input and print for even palindrome number. Next, use the calling vector function that generates all the even number satisfied for palindrome.

Step 6: Finally, we are using the for loop where variable i iterate through vector result and print all satisfied palindrome between 0 and given integer n.

### Example

In the following example, we will be going to check whether the input integer is satisfied for even palindrome or not.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> test(int n) {
vector<int> result;
// Apply the condition for even palindrome numbers between 50-100
// using += 2 checks for an even index positioning
for (int i = 0; i < n; i += 2)
{
string str = to_string(i);
string rvs(str.rbegin(), str.rend());
if (str == rvs)
{
result.push_back(i);
}
}
return result;
}
//Start the main function
int main()
{
int n = 2000;
cout << "\nEven palindrome numbers between 0 to " << n << ":\n";
vector<int> result = test(n);
for (int i : result)
{
cout << i << ' ';
}
return 0;
}


### Output

Even palindrome numbers between 0 to 2000:
0 2 4 6 8 22 44 66 88 202 212 222 232 242 252 262 272 282 292 404 414 424 434 444 454 464 474 484 494 606 616 626 636 646 656 666 676 686 696 808 818 828 838 848 858 868 878 888 898


## Conclusion

We discussed two methods to solve the problem statement based on even number of digits is palindrome or not. The first method simply uses the for loop and if-statement to set the condition for an even number whereas the second method uses a vector library to provide some of the STL function.

Updated on: 17-Aug-2023

75 Views