C++ Program to Check Whether a Number or String is a Palindrome or not (5 Ways)
In this program, we learn how to check whether a number or string is a Palindrome or not using C++ programming language.
What is Palindrome?
For example:- radar, it will spell same whether we read it from start or end. There are many more like dad, Malayalam etc.
We will write the C++ program to check for both number and string.
we will discuss Five ways to write code for it:-
- Checking a number is palindrome or not
- Using loop
- Using recursion
- Checking a string is palindrome or not
- Using loop
- Using recursion
- Using an inbuilt function.
Let’s discuss all the ways one by one.
C++ Programming Code to Check Whether a Number or String is a Palindrome or not
Checking a number is palindrome or not
Using Loop
We will use the while loop to make this program.
We will take individual digits from the given number and then adding it to the product of reversed number and 10.
Code:-
#include <iostream> using namespace std; int main(){ int num,rem,rev=0,temp; cout<<"Enter the Number: "; cin>>num; temp=num; while(temp>0){ rem=temp%10; rev=(rev*10)+rem; temp=temp/10; } if(num==rev) cout << num << " is a Palindrome Number." << endl; else cout << num << " is not a Palindrome Number." << endl; return 0; }
Output:-
85458 is a Palindrome number.
Using Recursion
In this program, we will use reverseNumber() recursive function to get the reverse of the given number and then compare it to the given number.
Code:-
#include <iostream> #include <math.h> using namespace std; int reverseNumber(int num){ int digits = (int) log10(num); // Base condition if(num == 0) return 0; return ((num%10 * pow(10, digits)) + reverseNumber(num/10)); } int main(){ int num,rev,sum=0,temp; cout<<"Enter the Number: "; cin>>num; rev = reverseNumber(num); if(num==rev) cout << num << " is a Palindrome Number." << endl; else cout << num << " is not a Palindrome Number." << endl; return 0; }
Output:-
569965 is a Palindrome Number.
Checking a string is palindrome or not
Using loop
In this program, we have used the for loop.
Inside for loop, it checks the elements which are equidistant from the center. And at any point they differ, it raises the flag and breaks from the loop.
#include <iostream> #include <string.h> using namespace std; int main(){ char str[20]; int flag = 0; cout << "Enter a string: "; cin >> str; int len = strlen(str); for(int i=0;i < len ;i++){ if(str[i] != str[len-i-1]){ flag = 1; break; } } if (flag) { cout << str << " is not a palindrome." << endl; } else { cout << str << " is a palindrome." << endl; } return 0; }
Output:-
radar is a palindrome.
Using Recursion
In this program, I have used the isPalindrome() recursive function.
In which, it compares the first and the last element string( passed in the parameters and it stops when first element and last element don’t match or start and end become equal.
#include <iostream> #include <string.h> using namespace std; bool isPalindrome(const string &str, int start, int end) { if (start >= end) return true; if (str[start] != str[end]) return false; return isPalindrome(str, ++start, --end); } int main(){ char str[20]; int flag = 0; cout << "Enter a string: "; cin >> str; int len = strlen(str)-1; if(isPalindrome(str,0,len)) cout << str << " is a Palindrome." << endl; else cout << str << " is not a Palindrome." << endl; return 0; }
Output:-
malyaylam is a Palindrome.
Using the in-build function
In this program, I have used the reverse() in-build function, which will reverse the given string. And later we compare it with the given string.
Code:-
#include<iostream> #include<string.h> #include<algorithm> using namespace std; int main() { string str, rev; cout << "Enter a string : "; cin >> str; rev = str; // inbuild reverse function reverse(rev.begin(), rev.end()); if (str == rev) { cout << str << " is a palindrome." << endl; } else { cout << str << " is not a palindrome." << endl; } return 0; }
Output:-
radar is a Palindrome.
You can learn about many other C++ Programs Here.
Best Books for learning C++ programming language with Data Structure and Algorithms.
Amit Rawat
Latest posts by Amit Rawat (see all)
- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019