Palindrome Check
Program to check if a number is palindrome
BeginnerTopic: Loop Programs
C++ Palindrome Check Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0, remainder, original;
cout << "Enter a number: ";
cin >> num;
original = num;
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed) {
cout << original << " is a palindrome" << endl;
} else {
cout << original << " is not a palindrome" << endl;
}
return 0;
}Output
Enter a number: 121 121 is a palindrome
Understanding Palindrome Check
A palindrome number reads the same forwards and backwards. We reverse the number using the same technique as the reverse program, then compare the original with the reversed number. If they match, it's a palindrome.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.