Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7187857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:56:22+00:00 2026-05-28T18:56:22+00:00

I am trying to solve the following problem: Find the largest palindrome made from

  • 0

I am trying to solve the following problem:

Find the largest palindrome made from the product of two 3-digit numbers.

I have the following Java code:

public static void main(String[] args) {
        int a = 999, b = 999;
        for(int i = 100; i <= a; i++) {
            for(int j = 100; j <= b; j++) {
                checkPalindrome(i*j, i, j);
            }
        }
    }

    public static void checkPalindrome(int n, int a, int b) {
        String s = "" + n;
        boolean palindrome = false;
        int j = s.length()-1;

        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) != s.charAt(j))
                break;
            j -= i;
        }

        if(palindrome)
            System.out.println(n + ", " + a + ", " + b);
    }

I’m still lacking the change of the “palindrome” variable but at the moment if I run it I get a String index out of range on line 28 which is the j -= i I just don’t understand why this is happening I mean, I get that the difference is resulting in a number lower than 0 but I can’t figure out WHY it happens. Could someone please explain me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:56:23+00:00Added an answer on May 28, 2026 at 6:56 pm

    Your method can be improved like this. The condition in for loop i<=j reduced number of iterations too.

    public static void checkPalindrome(int n, int a, int b) {
        String s = "" + n;
        boolean palindrome = false;
        int j = s.length()-1;
    
        for(int i = 0; i <= j; i++){
            if(s.charAt(i) != s.charAt(j))
                break;
            j --;
        }
    
        if(palindrome)
            System.out.println(n + ", " + a + ", " + b);
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve the following problem. Lets say you have the the
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
I am trying to solve the following problem but cannot find an elegant solution.
I'm trying to solve the following real-life problem you might have encountered yourselves: You
I am trying to solve this problem. I have a series of SELECT statements
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
I'm trying to find a more or less foolproof way to solve the following
I'm trying to use STL to solve the following problem (I don't want to
Here is the problem I am trying to solve. I have a table where
I am trying to find out how to solve the problem for serializing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.