Write a C++ Program to Check the Number is Palindrome with an example. Any number can be palindrome if the reverse of that is equal to the actual. For instance, 121 because you get the same number when you reverse it.
C++ Program to Check Number is Palindrome using While loop
In this example, we assigned the actual value to temp. Next, we revered that temp value. Finally, we used the If condition to check whether the given value equals reverse and print the palindrome.
#include<iostream> using namespace std; int main() { int number, temp, reminder, reverse = 0; cout << "Please Enter the Number to Check Palindrome or Not = "; cin >> number; temp = number; while (temp > 0) { reminder = temp % 10; reverse = reverse * 10 + reminder; temp = temp / 10; } cout << "\nReverse of given Number = " << reverse << "\n\n"; if(number == reverse) { cout << number << " is a Palindrome Number"; } else { cout << number << " is Not a Palindrome Number"; } return 0; }
Let me check with the other value. I suggest you refer to the Reverse a Number article.
Please Enter the Number to Check Palindrome or Not = 191
Reverse of given Number = 191
191 is a Palindrome Number
C++ Program to Check Number is Palindrome using For Loop
#include<iostream> using namespace std; int main() { int number, temp, reminder, reverse = 0; cout << "Please Enter the Number = "; cin >> number; for(temp = number; temp > 0; temp = temp / 10) { reminder = temp % 10; reverse = reverse * 10 + reminder; } cout << "\nReverse of given Number = " << reverse << "\n\n"; if(number == reverse) { cout << number << " is a Palindrome Number"; } else { cout << number << " is Not"; } return 0; }
Please Enter the Numbert= 1331
Reverse of given Number = 1331
1331 is a Palindrome Number
This C++ program helps to find Palindrome number using Functions.
#include<iostream> using namespace std; int reverseNumbers(int number) { int reminder, reverse = 0; for(; number > 0; number = number / 10) { reminder = number % 10; reverse = reverse * 10 + reminder; } return reverse; } int main() { int number, reverse = 0; cout << "Please Enter the Number = "; cin >> number; reverse = reverseNumbers(number); cout << "\nReverse of given Number = " << reverse << "\n\n"; if(number == reverse) { cout << number << " is a Palindrome Number"; } else { cout << number << " is Not"; } return 0; }
Please Enter the Number = 191
Reverse of given Number = 191
191 is a Palindrome Number
C++ Program to find the Number is Palindrome using Recursion
In this code, the int reverseNumbers method reverses the given number. Here, reverseNumbers(number/10) line recursively calls the int reverseNumbers method with an updated value.
#include<iostream> using namespace std; int reverseNumbers(int number) { static int reminder, reverse = 0; if(number > 0) { reminder = number % 10; reverse = reverse * 10 + reminder; reverseNumbers(number / 10); return reverse; } else return 0; } int main() { int number, reverse = 0; cout << "Please Enter the Number = "; cin >> number; reverse = reverseNumbers(number); cout << "\nReverse of given = " << reverse << "\n\n"; if(number == reverse) { cout << number << " is a Palindrome"; } else { cout << number << " is Not"; } return 0; }
Please Enter the Number = 4554
Reverse of given = 4554
4554 is a Palindrome
C++ Program to Print Palindrome Numbers from 1 to 100
This example allows the user to enter the minimum and maximum values. Next, it prints the palindrome numbers between min and max values.
#include<iostream> using namespace std; int main() { int minimum, maximum, number, temp, reminder, reverse; cout << "\nPlease Enter the Minimum Value = "; cin >> minimum; cout << "\nPlease Enter the Maximum Value = "; cin >> maximum; cout << "\nBetween " << minimum << " and " << maximum << " = "; for(number = minimum; number <= maximum; number++ ) { temp = number; reverse = 0; while (temp > 0) { reminder = temp % 10; reverse = reverse * 10 + reminder; temp = temp / 10; } if(number == reverse) { cout << number << " "; } } return 0; }
Please Enter the Minimum Value = 100
Please Enter the Maximum Value = 1000
Between 100 and 1000 = 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999