C++ Program - Check whether a Number is Palindrome or not
A number is known as a Palindrome number if the reverse of the number is same as the number. For example, 121 is a palindrome number, but 123 is not a palindrome number.
Example: Check Palindrome Number
In the example below, the number called MyNum is checked for palindrome number. The MyNum is first stored in a variable called Num. A while loop is used on this variable to perform following operations:
- last digit of the Num variable is estimated using Num % 10 and stored in digit variable.
- reverse number is built using revNum * 10 + digit.
- last digit is removed from Num variable using Num / 10.
Finally, MyNum is compared with revNum to check whether the number is palindrome or not.
#include <iostream> using namespace std; static void Palindrome(int MyNum) { int revNum = 0; int Num = MyNum; while(Num > 0){ int digit = Num % 10; revNum = revNum * 10 + digit; Num = Num / 10; } if (MyNum == revNum){ cout<<MyNum<<" is a Palindrome number.\n"; } else { cout<<MyNum<<" is not a Palindrome number.\n"; } } int main() { Palindrome(12521); Palindrome(9779); Palindrome(1000); return 0; }
The above code will give the following output:
12521 is a Palindrome number. 9779 is a Palindrome number. 1000 is not a Palindrome number.
Recommended Pages
- C++ Program - To Check Prime Number
- C++ Program - Bubble Sort
- C++ Program - Selection Sort
- C++ Program - Maximum Subarray Sum
- C++ Program - Reverse digits of a given Integer
- C++ - Swap two numbers
- C++ Program - Fibonacci Sequence
- C++ Program - Insertion Sort
- C++ Program - Find Factorial of a Number
- C++ Program - Find HCF of Two Numbers
- C++ Program - Merge Sort
- C++ Program - Shell Sort
- Stack in C++
- Queue in C++
- C++ Program - Find LCM of Two Numbers
- C++ Program - To Check Armstrong Number
- C++ Program - Counting Sort
- C++ Program - Radix Sort
- C++ Program - Find Largest Number among Three Numbers
- C++ Program - Print Floyd's Triangle