-->





Find Palindrome number in c++

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:


  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int palindrome, reverse=0;
  6.     cout<<"Enter number:  ";
  7.     cin>>palindrome;
  8.     int num=0,key=palindrome;
  9. for(int i=1;palindrome!=0;i++){

  10.     num=palindrome%10;
  11.     palindrome=palindrome/10;
  12.     reverse=num+(reverse*10);
  13.               }

  14.    if(reverse==key){
  15.    cout<<key<<" is a Palindrome Number";
  16.             }
  17.             else{
  18.    cout<<key<<"is NOT a Palindrome Number";
  19.             }
  20. return 0;
}
Dry Running The Code with respect to variable values and iteration
Let input is 121
  1. Before for loop variable values are
  2. palindrome=121;
  3. key=121;
  4. reverse=0;
  5. num=0;

After for loop


1st iterationwhen   i = 1

  1. num=1;
  2. palindrome=12;
  3. reverse=1;
2nd iterationwhen   i = 2

  1. num=2;
  2. palindrome=1;
  3. reverse=12;
3rd iteration
when   i = 3

  1. num=1;
  2. palindrome=0;
  3. reverse=121;
So palindrome=0; loop will break and program control will transfer to if else statement
Image View Of Code: click on image to view large

c++ program to find palindrome number
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

5 comments:

  1. this is of gr8 help. thank u a lot!!!

    ReplyDelete
  2. Key and palindrome variables made program complex. Let assume entered no. is 'n'.

    ReplyDelete
  3. Palindrome Number Program in C++

    A 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.

    ReplyDelete