0

I am making the program forthe largest palindrome made from the product of two 3-digit numbers. But program is giving product of 999*999=998001 Can anyone tell what's wrong in this code?

Program-

public class abc {
    public static void main(String[] args) 
    {
        int p=0,temp=0;

        for(int i=100;i<=999;i++)
        {
            for(int j=100;j<=999;j++)
            {
                p=i*j;
                StringBuilder sb=new StringBuilder(Integer.toString(p));

                sb.reverse();
                if((sb.toString()).equals(Integer.toString(p))  && p>temp)
                {
                    temp=p;
                }
            }
        }       
        System.out.println(temp);
    }
}
Stones
  • 13
  • 7

1 Answers1

0

You have an extra ; :

if((sb.toString()).equals(Integer.toString(p))  && p>temp);
                                                          ^

It ends the if statement, so the following block, with temp=p; is always executed.

Remove it and you'll get

906609
Eran
  • 387,369
  • 54
  • 702
  • 768