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 6745497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:12:44+00:00 2026-05-26T12:12:44+00:00

I am learning Java using Java how to program (Deitel and Deitel). Right now

  • 0

I am learning Java using “Java how to program” (Deitel and Deitel).
Right now I´m stuck solving an exercise that wants me to print out a table with all possible values of “pythagoran tripples” under 500. I am supposed to use a nested “for-loop” to check all possibilities. In other words: a, b, and c has to be integers. The following expression has to be true: a2 + b2 = c2, and the program should print a table with all possible combinations ( with c < 500 ). I just can´t figure this out. Can anyone please help me?
My code, which only prints out the first combination ( 3 4 5 ) is as follows:

    public class Pythagoras 
            {
        public static void main(String[] args) 
            {

            for (int a = 3, b = 4, c = 5; (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) && (c <= 500); c++)
        {
        System.out.printf("%d %20d %20d", a, b, c);
        }

        }

    }
  • 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-26T12:12:45+00:00Added an answer on May 26, 2026 at 12:12 pm

    Your code only prints 3 4 5 because it only runs 1 iteration of the for loop.
    In your for loop, you enlarge c each iteration, but you don’t change a and b.
    That means that after the first iteration, it will evaluate 3^2 + 4^2 == 6^2, which returns false and it thus exits the for loop.

    To solve this, you could use three nested for loops like this:

    for(int a = 1; a < 500; a++){
       for(int b = 1; b < 500;b++){
           for(int c = 1; c < 500;c++){
               if(Math.pow(c,2) == Math.pow(a,2) + Math.pow(b,2){
                    // code execution
               }
           }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning C++ right now using Bruce Eckel's Thinking in C++ and I'm in
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
Forgive my ignorance - still learning here. I am using Eclipse Ganymede (Java 1.5)
So I'm learning java, and I have a question. It seems that the types
I am learning Java using the book Java: The Complete Reference. Currently I am
I am learning Java and I would like to know how you can print
i just started learning swings. And thought of trying out a simple program, but
Well..I am learning java now and I am curious to know will this yield
I am struggling to port a Perl program to Java, and learning Java as
I am looking at learning to program a Java EE/SE web application with the

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.