Di Posting Oleh : Simple Learning
Kategori : palindrome number dry run palindrome number example palindrome number in c++ palindrome number logic
What is a palindrome number?
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
0 Response to "Find Palindrome number in c++"
Post a Comment