Tuesday, 5 June 2018

Project Euler Problem 4:Largest palindrome product

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.


Solution:-


 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
44
45
public class Problem_4{

     public static void main(String []args){
        
        int pal=0;
        int fdigit=0;
        int sdigit=0;
        
        for(int i=100;i<999;i++){
            
            for(int j=100;j<999;j++){
                
                int temp= i*j;
                if(isPalindrome(temp)){
                    if(temp>pal){
                        pal=temp;
                        fdigit=i;
                        sdigit=j;
                    }
                }
                
            }
            
        }
        
        System.out.println("Largest palindrome is "+pal+"\n First Digit is"+fdigit+"\n Second Digit is"+sdigit);
     }
     
     static boolean isPalindrome(int a){
         int rev=0;
         int temp=a;
         while(temp>0){
             rev=temp%10 + rev*10;
             temp=temp/10;
         }
         
         if(rev==a)
            return true;
        else
            return false;
         
         
     }
     
}

No comments:

Post a Comment