C++: Learning the Basics – Palindromes

Situation: User enters a number and program displays whether the number is a palindrome or not.

What is a palindrome number?

A palindromic number is a number that is the same when written forwards or backwards. Examples: 121, 222, 101, 66.

#include <iostream.h>
#include <conio.h>
void main()

{
  clrscr();
  int num, org, rev=0, rem;
  
  cout<<"\n\t Enter a number: ";
  cin>>num;
 
  org=num;
 
  while (num>0)
  {
          rem=num%10;
          rev=rev*10+rem;
          num=num/10;
  }
  
  cout<<"\n\t Reverse number: "<<rev;

  if (org==rev)
  {
          cout<<"\n\t Palindrome";
  }
  else
  {
          cout<<"\n\t Not a palindrome";
  }

  getch();

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s