-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestPalindrome.java
43 lines (40 loc) · 985 Bytes
/
LargestPalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.TreeSet;
public class LargestPalindrome {
public static void main(String[] args) {
int maxPalindrome =-1;
for (int i = 999; i > 0 ; i--)
{
for (int j = 999; j > 0; j--)
{
int temp=i*j;
if(isPalindrome(temp))
{
if(temp>maxPalindrome)
maxPalindrome = temp;
}
}
}
System.out.println(maxPalindrome);
}
public static boolean isPalindrome(int n){
boolean flag=true;
int cn=n;
int nod=0;
while(cn>0)
{
int ld=cn%10;
nod++;
cn/=10;
}
String s=String.valueOf(n);
for (int i = 0; i <= nod/2; i++)
{
if(s.charAt(i)!=s.charAt(nod-1-i))
{
flag=false;
}
if(flag==false)break;
}
return flag;
}
}