C++ Program to Check Whether a Number is Palindrome or Not
Last Updated :
13 Jan, 2025
A palindrome number is a number that remains the same even if its digits are reversed. In this article, we will learn how to check a given number is a palindrome or not in C++.
The easiest way to check if a number is a palindrome is to simply reverse the original number, then check if both numbers are equal. Let's take an example:
C++
#include <iostream>
using namespace std;
int main() {
int n=1221;
int t = n;
int rev = 0;
// Calculate reverse number rev of a given number n
while (t > 0) {
int dig = t % 10;
rev = rev * 10 + dig;
t /= 10;
}
// Compare number n with reverse number rev
if (n==rev)
cout << "Palindrome.";
else
cout << "Not Palindrome.";
return 0;
}
Explanation: In this program, we reverse the number n using while loop and % modulo operator and storing the reverse number into rev variable. Then compare the original number n with reverse number rev. If they are equal print Palindrome otherwise print Not Palindrome.
There are also some other methods to check for a palindrome number in C. They are as follows:
Using Two Pointers After String Conversion
In two-pointer technique, first convert the number into a string using to_string() function and then take two pointers: one pointing at the start of the string and the other at the end of the string. Compare the characters they point to while moving these pointers towards each other. If all the characters match by the time pointers meets, the number is a palindrome, otherwise it is not.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 1221;
// Convert number into string
string s = to_string(n);
int l = 0;
int r = s.size()-1;
// Compare both characters
// pointed by l and r pointer
while (l<r) {
if (s[l] != s[r]) {
cout << "Not Palindrome.";
return 0;
}
// Move both pointers
// towards center.
l++, r--;
}
// All characters matched
// means number is palindrome.
cout << "Palindrome.";
return 0;
}
Note: We can also use to check number n is palindrome if generate a reverse string of a generated string, then check both strings are equal to check number is palindrome.
Using Stack
The approach to push all digits of a number into stack storing them in reverse order. Then compare the digits of stack with number's corresponding digits. If any mismatch occurs, the number is not palindrome, otherwise, it is palindrome.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 1231;
stack<int> st;
int t = n;
// Insert each digit from the last of
// number into stack
while (t > 0) {
st.push(t % 10);
t /= 10;
}
while (!s.empty()) {
// Check stack top element is equal
// to last digit of a Number
if (st.top() != n % 10) {
cout << "Not Palindrome.";
return 0;
}
// Remove the top digit of
// the stack
s.pop();
// Remmove last digit of n
n /= 10;
}
// All character matched means
// number is palindrome.
cout << "Palindrome.";
return 0;
}
Explanation: In this program, pushing all digits of a number into stack. Compare top element of stack s with last digit of a number n and if any mismatch happen means number n is not palindrome otherwise number is palindrome.
Similar Reads
Recursive program to check if number is palindrome or not Given a number, the task is to write a recursive function that checks if the given number is a palindrome or not. Examples: Input: 121Output: yes Input: 532Output: no The approach for writing the function is to call the function recursively till the number is wholly traversed from the back. Use a te
4 min read
C++ Program to Check if a Given String is Palindrome or Not A string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++.ExamplesInput: str = "ABCDCBA"Output: "ABCDCBA" is palindromeExplanation: Reverse of the string str is "ABCDCBA". So,
4 min read
Program to check if an Array is Palindrome or not using STL in C++ Given an array, the task is to determine whether an array is a palindrome or not, using STL in C++. Examples: Input: arr[] = {3, 6, 0, 6, 3} Output: Palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Not Palindrome Approach: Get the reverse of the Array using reverse() method, provided in STL.Initial
2 min read
C++ Program To Check If A Singly Linked List Is Palindrome Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
9 min read
XOR and OR of all N-digit palindrome number Given an integer N. The task is to find the XOR and OR of all N digit palindromic numbers.Examples Input: 3 Output: XOR = 714 and OR = 1023Input: 4 Output: XOR = 4606 and OR = 16383 Approach: Find the starting and ending number of N-digit palindromic number by: starting number = pow(10, n - 1) endin
7 min read