Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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.
- 2 digits : 10->99
- 3 digits : 100->999
- */
- import javax.swing.*;
- public class LargestPalindrome{
- public static void main(String [] args){
- long largestPalindrome=0;
- long product;
- for(long i=100;i<1000;i++)
- {
- for (long j= 100;j<1000;j++)
- {
- product = i*j;
- if (Palindrome(product))
- {
- if (product>largestPalindrome)
- {
- largestPalindrome=product;
- }
- }
- }
- }
- System.out.println("Largest palindrome made from the product of two 3-digit numbers: "+largestPalindrome);
- }
- public static boolean Palindrome(long aInt)
- {
- String str1= Long.toString(aInt);
- String str2="";
- int length = str1.length();
- for (int i=length-1;i>=0;i--)
- {
- str2 += str1.charAt(i);
- }
- //System.out.println(str1+"\n"+str2);
- if (str1.equalsIgnoreCase(str2))
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement