this program is to find the largest palindrome of 3 digit numbers.this code is workig for two digit numbers but it is not working for 3 digit numbers.
``

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int i,j,con=0;
 long int num=0,res=0;
clrscr();
for(i=999;i>900;i--)
{
  for(j=999;j>i;j--)
  {
   num=(i*j);
   con=pal(num);
   if(con==0&&num>=res)
    res=num;
  }
}
printf("\npalindrome is %lu",res);
getch();
}
int pal(long int a)
{
long int b=0,c=a;
 while(a!=0)
 {
  b=(b*10)+(a%10);
  a=a/10;
 }
 if(c==b)
    return 0;
 else
    return 1;
}

`
on executing above code the output is 32723 but the out should be 906609..

Recommended Answers

All 5 Replies

Hi vallarakesh,

on executing above code the output is 32723 but the out should be 906609..

Check the modify code below:

    #include<stdio.h>
    #include<conio.h>
    #include<math.h>

    int pal(long int);   /* declare your function */

    int main() {         /* use int main() not void main() */
        int i, j, con = 0;
        long int num = 0, res = 0;

          //clrscr();    //??

          for ( i = 999 ; i > 900 ; i-- ) {
            for ( j = 999 ; j > i ; j-- ) {
                num = ( i * j );
                con = pal(num);
                if ( con == 0 && num >= res )
                 res = num;               
            }
        }
        printf( "\npalindrome is %lu", res );
          //getch(); //??
        return 0;
      }

  int pal( long int a ) {
        long int b = 0, c = a;
          while ( a != 0 ) {
            b = ( b * 10 ) + ( a %10 );
            a = a / 10;
        }
        if ( c == b ) return 0;
          else return 1;
      }  

999999

hi 2teez.
as i said earlier the output is not correct.the outpput for the edited code is 31913.

You have not given any reason why that would be expected answer and not for example 999999

commented: the reason is simple.we need the largest palindrme which is a product of two 3digit numbers. +0

Hi vallarakesh,
I ran this modified code on codepad.org, an online complier. I got the output:

palindrome is 906609

You can try it

    #include<stdio.h>
    #include<math.h>

    int pal(long int);   /* declare your function */

    int main() {         /* use int main() not void main() */
        int i, j, con = 0;
        long int num = 0, res = 0;

          //clrscr();    //??

          for ( i = 999 ; i > 900 ; i-- ) {
            for ( j = 999 ; j > i ; j-- ) {
                num = ( i * j );
                con = pal(num);
                if ( con == 0 && num >= res )
                 res = num;               
            }
        }
        printf( "\npalindrome is %lu", res );
          //getch(); //??
        return 0;
      }

  int pal( long int a ) {
        long int b = 0, c = a;
          while ( a != 0 ) {
            b = ( b * 10 ) + ( a %10 );
            a = a / 10;
        }
        if ( c == b ) return 0;
          else return 1;
      }  
commented: thank you 2teez..it is working in codepad but while i am executing the same code in my PC the output is 31913.may i know what is the problem is. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.