Today we will learn the Palindrome program in C++ and Palindrome String program in C++.So, Before start learning, you should have knowledge of palindrome.

What is Palindrome?

The number or string is said to be palindrome if the reverse of that number or string is the original number or string itself.

For example: ‘121’ and ‘madam’

In the above examples, we can observe that if we reverse the number 121 and string madam we will get the same number and string. So, the number and the given string is called Palindrome.

There are so many Program methods to get the Palindrome of number and string we will see it one by one.

1. Palindrome Program in C++ Using While Loop

In this program, we will ask the user to input a number which the user wants to check whether the given number is palindrome or not using while loop.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include<iostream>
using namespace std;
int main()
{
int num, reverse_num = 0, temp_num;
cout << "Enter random number to check palindrome value:";
cin >> num;
temp_num = num;
while (num != 0)
{
reverse_num = reverse_num * 10;
reverse_num= reverse_num + num%% 10;
num = num / 10;
}
if(temp_num == reverse_num)
{
cout << "Given number is palindrome";
}
else
{
cout << "Given number is not a palindrome";
}
return 0;
}</pre>
</div>

Output:

program for palindrome in c++
program for palindrome in c++

Here the user enters the number and the number is stored in the variable num. Then the number is assigned to the variable tempn and the reverse variable is stored in the reversen.

If the reversen is equal to tempn then the given number is Palindrome otherwise, the number is not Palindrome.

2. Palindrome String Program in C++

In this program we will put some string in the function then the program will check whether the string is Palindrome or not.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <iostream>
#include<string.h>
using namespace std;
int main()
{
    char string1[20];
    int i, length;
    int flag = 0;
    cout << "Enter a string: "; cin >> string1;
    length = strlen(string1);
    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
            flag = 1;
            break;
   }
}
    if (flag) {
        cout << string1 << " is not a palindrome" << endl; 
    }    
    else {
        cout << string1 << " is a palindrome" << endl; 
    }
    system("pause");
    return 0;
}
</pre>
</div>

Output:

palindrome program in c++

In the above program, we have given input string as madam. During compilation of the program, the compiler compares the input string with the reverse of the input string.

If the input string is equal to the reverse string then the compiler will print “String is Palindrome” else compiler will print “String is not Palindrome”.

3. Palindrome Program in C++ Using For Loop

In this program, we will use for loop instead of while loop. Don’t be afraid the logic is same only the loop is changed.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include<iostream>
using namespace std;
int main()
{
int num, remainder_num, reverse_num = 0, i, temp_num;
cout << "Enter random number to check palindrome value:";
cin >> num;
temp_num = num;
for(i = num; i >0;)
{
remainder_num= i %% 10;
reverse_num = remainder_num+ reverse_num * 10;
i = i/ 10;
}
if(temp_num == reverse_num)
{
cout << "Given number is palindrome";
}
else
{
cout << "Given number is not a palindrome";
}
return 0;
}</pre>
</div>

Output:

palindrome program in c++
program for palindrome in c++

In the above program when the user enters the number, the program first reverses the number and then check whether the number is palindrome or not.

4. Using Do While Loop

In this program, we will use a do-while loop to find the palindrome of a number.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include<iostream>
using namespace std;
int main()
{
int num, reverse_num = 0, temp_num;
cout << "Enter random number to check palindrome value:";
cin >> num;
temp_num = num;
do
{
reverse_num = reverse_num * 10;
reverse_num = reverse_num+ num %% 10;
num = num / 10;
}while(num != 0);
if(temp_num == reverse_num)
{
cout << "Given number is palindrome";
}
else
{
cout << "Given number is not a palindrome";
}
return 0;
}</pre>
</div>

Output:

palindrome string program in c++

In the above program, to check whether the number is palindrome or not we have used the do-while loop. This program is the same as while loop, But only the difference is While loop first checks the condition and then executes the loop. But in the do-while loop, the program first executes the loop and then checks the condition.

During the execution, if the condition is true then only loop will be executed else the loop will be terminated.

Once the reverse value is detected then the compiler will compare that value with temporary value.

Also Read: