If the digits of a numbers reversed and number remain the same then it is called palindrome number
For example:
Digits from 0 to 9 are palindrome numbers and
22 ,33, 44, 121, 12321, 131 etc..
for more information click here palindrome number concept by Wikipedia
Simple c++ code to find number is palindrome or not:
- #include<iostream>
- using namespace std;
- int main()
- {
- int palindrome, reverse=0;
- cout<<"Enter number: ";
- cin>>palindrome;
- int num=0,key=palindrome;
- for(int i=1;palindrome!=0;i++){
- num=palindrome%10;
- palindrome=palindrome/10;
- reverse=num+(reverse*10);
- }
- if(reverse==key){
- cout<<key<<" is a Palindrome Number";
- }
- else{
- cout<<key<<"is NOT a Palindrome Number";
- }
- return 0;
Dry Running The Code with respect to variable values and iterationLet input is 121
- Before for loop variable values are
- palindrome=121;
- key=121;
- reverse=0;
- num=0;
After for loop
1st iterationwhen i = 1
- num=1;
- palindrome=12;
- reverse=1;
- num=2;
- palindrome=1;
- reverse=12;
when i = 3
Image View Of Code: click on image to view large
Logic Explanation:
- num=1;
- palindrome=0;
- reverse=121;
Image View Of Code: click on image to view large
Palindrome number in c++ |
Logic Explanation:
- We already known that if reverse of a number is equal to the same number than it is palindrome number
- Keeping in mind this certain variables and a loop use to get the reverse of a number which stores in variable 'reverse'
- After that using if else we check if reverse is equal to original number than it is a palindrome number otherwise it is not a palindrome number
this is of gr8 help. thank u a lot!!!
ReplyDeletedude you rock!
ReplyDeletethanx brother
ReplyDeleteKey and palindrome variables made program complex. Let assume entered no. is 'n'.
ReplyDeletePalindrome Number Program in C++
ReplyDeleteA Palindrome number is a number that remains the same when its digits are reversed. Like 16461, for example: we take 121 and reverse it, after revers it is same as original.