Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,955 questions

51,897 answers

573 users

How to find the largest palindrome made from the product of two 2-digit numbers in Java

3 Answers

0 votes
public class MyClass {
    private static boolean isPalindrome(int num) {
    	int temp = num;
    	int rev = 0;
    
    	while (temp > 0) {
    		rev = rev * 10 + (temp % 10);
    		temp /= 10;
    	}
    
    	return rev == num;
    }
    private static int getLargestPalindromeOfTwo2digitNumbers() {
    	int largestPalindrome = -1;
    
    	for (int num1 = 10; num1 < 100; num1++) { // 10 to 99
    		int product = 9 * num1;
    		for (int num2 = 10; num2 < 100; num2++) { // 10 to 99
    			product += num1;
    			if (product > largestPalindrome && isPalindrome(product) != false) {
    				largestPalindrome = product;
    			}
    		}
    	}

	    return largestPalindrome;
    }
    public static void main(String args[]) {
        System.out.print(getLargestPalindromeOfTwo2digitNumbers());
    }
}





/*
run:
      
9009
      
*/

 



answered Oct 16, 2023 by avibootz
0 votes
public class MyClass {
    static int LargestNum1 = 0, LargestNum2 = 0; 
    
    private static boolean isPalindrome(int num) {
    	int temp = num;
    	int rev = 0;
    
    	while (temp > 0) {
    		rev = rev * 10 + (temp % 10);
    		temp /= 10;
    	}
    
    	return rev == num;
    }
    private static int getLargestPalindromeOfTwo2digitNumbers() {
    	int largestPalindrome = -1;
    
    	for (int num1 = 10; num1 < 100; num1++) { // 10 to 99
    		int product = 9 * num1;
    		for (int num2 = 10; num2 < 100; num2++) { // 10 to 99
    			product += num1;
    			if (product > largestPalindrome && isPalindrome(product) != false) {
    			    LargestNum1 = num1;
                    LargestNum2 = num2;
    				largestPalindrome = product;
    			}
    		}
    	}

	    return largestPalindrome;
    }
    public static void main(String args[]) {
        int result = getLargestPalindromeOfTwo2digitNumbers();
        
        System.out.print(LargestNum1 + " x " +  LargestNum2 + " = " + result);
    }
}





/*
run:
      
91 x 99 = 9009
      
*/

 



answered Oct 16, 2023 by avibootz
0 votes
public class MyClass {
    private static boolean isPalindrome(int num) {
    	int temp = num;
    	int rev = 0;
    
    	while (temp > 0) {
    		rev = rev * 10 + (temp % 10);
    		temp /= 10;
    	}
    
    	return rev == num;
    }
    private static int getLargestPalindromeOfTwo2digitNumbers() {
    	int largestPalindrome = -1;
   
        for (int num1 = 99; num1 > 9; num1--) { // 99 to 10
            int product = 0;
            for (int num2 = num1; num2 > 9; num2--) { // 99 to 10
                product = num1 * num2;
                if (product > largestPalindrome && isPalindrome(product)) {
                    largestPalindrome = product;
                }
            }
        }
   
        return largestPalindrome;
    }
    public static void main(String args[]) {
        System.out.print(getLargestPalindromeOfTwo2digitNumbers());
    }
}





/*
run:
      
9009
      
*/

 



answered Oct 16, 2023 by avibootz
edited Oct 16, 2023 by avibootz
...