Pages

Thursday 17 December 2015

Palindrome number using C++


Palindrome number:

A number is said to palindrome if the digits of the number is reversed and it still remain the same.

Example:
  • Digits from $0$ to $9$ are all palindrome
  • $11$, $22$, $33$, $44$, $121$, $383$, $12321$, etc.
Algorithm

  1. Start the program
  2. Input a number to check palindrome or not
  3. Initialize the remainder variable to $0$
  4. Divide the input number by $10$ and store its remainder on a variable
  5. Store the quotient on the palindrome variable.
  6. Reverse the number and compare with the original input number
  7. If the reversed number is equivalent to original then it is palindrome otherwise not palindrome. 

#include"iostream"
using namespace std;
int main()
{
    int palindrome, reverse=0;

    cout<<"Enter a 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;
}



INPUT : Enter a number: $135$
OUTPUT: $135$ is not a Palindrome Number

INPUT : Enter a number: $52325$
OUTPUT: $52325$ is a Palindrome Number


No comments:

Post a Comment