Thread: to check whether a number is palindrome or not?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    to check whether a number is palindrome or not?

    hello!could not find the mistake in this program....please help!
    Code:
    //cpp program to check whether a number is palindrome or not
    #include<iostream.h>
    #include<conio.h>
    //the class
    class palindrome
    {
    	  public:
    	  int a[20];
    	  int n;
    	  int num;
    	  int compare(int [],int);
    };
    //function to compare the same array from backwards and from forward direction
    //if the comparison yields both the arrays to be same then return 1 else
    //return 0
    int palindrome::compare(int a[],int n)
    {
    	  int flag;
    	  for(int i=0,j=n-1;i<n/2;i++,j--)
    	  {
    			 if(a[i]==a[j])
    			 {
    					flag=1;
    			 }
    			 else
    			 {
    					flag=0;
    			 {
    	  }
    	  return(flag);
    }
    void main()
    {
    	  palindrome p1;
    	  cout<<"\n Enter number";
    	  cin>>p1.num;
    	  int r;
    	  int i=0;
    	  //code to place the digits of the number into an array
    	  while(p1.num>0)
    	  {
    			  r=p1.num%10;
    			  p1.a[i++]=r;
    			  p1.num/=10;
    	  }
    	  p1.n=i;
    	  if(p1.compare(p1.a,p1.n)==1)cout<<"\n Number is palindrome";
    	  else cout<<"\n Number is not palindrome";
    	  getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
                 else
                 {
                        flag=0;
                 {
    Well with two opening braces like that, it won't even compile.

    Also, your compare function only returns the result of the last comparison.
    Any number with the same first and last digit would return true.

    You need to exit early as soon as the comparison is false.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    13
    In the method intpalindrome::compare(inta[],intn),

    else
    {
    flag=0;
    {


    This leads the sytax error. Also, when ever condition matches then exit the loop.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    27
    thanks a lot for all your help!!

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Why force the digits into an array though when you can do the comparison all at one time? You can grab the # digits using logarithms, and you know that you can pop digits off the least significant side by using modulus and division, why not combine the two and meet halfway?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To check the String is palindrome or not.?
    By sash.arya in forum C Programming
    Replies: 7
    Last Post: 10-30-2012, 10:15 PM
  2. Function to check for palindrome
    By Kristyy_mariee in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2012, 07:08 PM
  3. Number Palindrome
    By Dontgiveup in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2011, 03:05 AM
  4. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  5. next palindrome number
    By kris.c in forum C Programming
    Replies: 5
    Last Post: 09-05-2006, 05:43 AM

Tags for this Thread