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
6 Years
Discussion Span
Last Post by 2teez

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

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

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