Palindrome Number in C++
Q. Write a C++ program to check Palindrome Number or Not.
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0, t;
cout << "Enter the number: ";
cin >> n ;
t = n;
while (t > 0)
{
sum = sum * 10 + t%10;
t = t/10;
}
if (n == sum)
{
cout << n <<" is a palindrome number." ;
}
else
{
cout << n <<" is not a palindrome number." ;
}
return 0;
}
Q. Write an algorithm to check Palindrome Number or Not.
1. Start
2. Input the integer number `n`
3. Initialize a variable `sum` to 0
(This will hold the reversed number)
4. Initialize a variable `t` with the value of `n`
(This will be used to extract digits without losing original number)
5. While `t` is greater than 0, do the following:
a. Extract the last digit of `t` by `t % 10`
b. Update `sum` as `sum * 10 + (t % 10)`
(This appends the extracted last digit to the reversed number)
c. Remove the last digit from `t` by `t = t / 10` (integer division)
6. After the loop ends, compare:
- If `sum` is equal to `n` then
Print "`n` is a palindrome number."
- Else
Print "`n` is not a palindrome number."
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com