/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
class Pro4
{
int palindrome(int x)
{
int n=x,rev=0;
while(n!=0)
{
rev=rev*10+n%10;
n=n/10;
}
if(x==rev)
return x;
else
return 0;
}
public static void main(String args[])
{
int lar=0,i=0,j=0,x,k=100,l=100;
Pro4 obj=new Pro4();
for(i=100;i<=999;i++)
for(j=100;j<=999;j++)
{
x=obj.palindrome(i*j);
if(x!=0)
{
lar=x;
k=i;
l=j;
}
}
System.out.println(lar+","+k+","+l);
}
}
Asked
Active
Viewed 50 times
-1

Vishal Gajera
- 4,137
- 5
- 28
- 55

darthvader xxx
- 9
- 3
-
1Just start with both values at `999` and iterate downwards; the first palindromic number will be the greatest. – Elliott Frisch Mar 28 '16 at 13:33
-
1What prevents you debugging your own code? – Raedwald Mar 29 '16 at 08:39
-
Possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Mar 29 '16 at 08:42
1 Answers
0
please, do following change in your code,
if(x != 0 && x>lar)
{
lar=x;
k=i;
l=j;
}
Output :
906609,913,993
UPDATED :
into your previous code which is only contain x!=0 condition,
906609
palindrome become at i = 913, j = 993,
then again next 580085
palindrome appear into double digit range at i = 995, j = 583
.
so, once 913,993(which gives 906609)
processing answer getting but lake of proper condition it will replace by 995,583(which gives 580085)
that's why tou were got 580085
.

Vishal Gajera
- 4,137
- 5
- 28
- 55
-
okay,but even if i didn't add x>lar then i should get 906609.why am i not getting then?could you please explain why it makes difference? – darthvader xxx Mar 28 '16 at 13:57
-
the wrong value 580085 must appear after 906609 that's why it's overwrites on our correct answer. – Vishal Gajera Mar 28 '16 at 13:59