0

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

3
Contributors
5
Replies
9
Views
4 Years
Discussion Span
Last Post by 2teez
Featured Replies
  • 1

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

0

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;
      }  
0

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

1

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

Comments
the reason is simple.we need the largest palindrme which is a product of two 3digit numbers.
1

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;
      }  
Comments
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.
This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.