In this post, we will learn how to check palindrome numbers using C++ Programming language.
A number is said to be palindrome if its reverse is equal to itself. For example: 101, 141, and 12321 etc.
If Original number = reverse, then the number is a palindrome number. We will check this with the help of while loop and if. . .else statement.
So, without further ado, let’s begin this tutorial.
C++ Program to Check Palindrome Number
// C++ Program to Check Palindrome Number #include <iostream> using namespace std; int main(){ int num, rev = 0, temp; // Asking for input cout << "Enter a number: "; cin >> num; temp = num; // Reverse the number while(temp > 0){ rev = rev * 10 + temp % 10; temp = temp / 10; } // Checking palindrome if (num == rev){ cout << num << " is a palindrome number."; } else{ cout << num << " is not a palindrome number."; } return 0; }
Output
Enter a number: 12321
12321 is a palindrome number.
How Does This Program Work ?
int num, rev = 0, temp;
In this program, we have declared three int data type variables named num, rev and temp.
// Asking for input cout << "Enter a number: "; cin >> num;
Then, the user is asked to enter a number to check the palindrome.
// Reverse the number while(temp > 0){ rev = rev * 10 + temp % 10; temp = temp / 10; }
We reverse the original number with the help of a while loop.
if (num == rev){ cout << num << " is a palindrome number."; } else{ cout << num << " is not a palindrome number."; }
Now, we check whether the reverse number is equal to the original number or not. If yes, then the entered number is a palindrome number and we display it on the screen using cout statement.
Otherwise, the program will display that the entered number is not a palindrome number.
Conclusion
I hope after going through this post, you understand how to check palindrome numbers using C++ Programming language.
If you have any problem regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.
Also Read: