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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:23:52+00:00 2026-06-11T14:23:52+00:00

Here is the question Find all Pythagorean Triples for side1, side2 and hypotenuse all

  • 0

Here is the question

Find all Pythagorean Triples for side1, side2 and hypotenuse all no longer than 500. Use a triple nested for loops that tries possibilities.

The below is my attempt

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int side1 = 0;
    int side2 = 0;
    int rightSide = 0;

    cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;

    for(int i=1;i<=500;i++)
    {
        side1++;
        //cout << side1 << endl;

        for(int a=1;a<=500;a++)
        {
            side2++;
            //cout << "side 2 " << side2 << endl;

            for(int c=1;c<=500;c++)
            {
                rightSide++;
                int rightSideSqr = rightSide*rightSide;
                int side1Sqr = side1*side1;
                int side2Sqr = side2*side2;

                if(rightSideSqr == side1Sqr+side2Sqr)
                {
                    cout << rightSideSqr << setw(15) << side1 << setw(10) << side2 << endl;
                 }


            }
        }
    }
}

But it gives no success, seems like an infinite loop. Please help.

Please Note: I am new to C++, I study it by my self. And, this is not an homework, I made the problem statement because it is the best way to express the issue.

EDIT

Right Side Side1 Side2

RUN SUCCESSFUL (total time: 1s)

EDIT 2

Working code

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //int side1 = 0;
    //int side2 = 0;
    //int rightSide = 0;

    cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;

    for(int i=1;i<=500;i++)
    {
        //side1++;
        //cout << side1 << endl;

        for(int a=1;a<=500;a++)
        {
            //side2++;
            //cout << "side 2 " << side2 << endl;

            for(int c=1;c<=500;c++)
            {
                //rightSide++;
                int rightSideSqr = c*c;
                int side1Sqr = i*i;
                int side2Sqr = a*a;

                if(rightSideSqr == (side1Sqr+side2Sqr))
                {
                    cout << rightSideSqr << setw(15) << i << setw(10) << a << endl;
                 }


            }
        }
    }
}
  • 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-06-11T14:23:53+00:00Added an answer on June 11, 2026 at 2:23 pm

    It’s not an infinite loop, it’s just a very slow finite loop. I/O is slow — you’re printing out 500*500 = 250,000 lines of text in the cout statement in the middle loop, and printing out 250,000 lines of text to the console is very, very slow. If you remove that print statement, it will execute much more quickly.

    Secondly, you have an error in your logic. The variables side1, side2, and rightSide never get reset to 0 at the appropriate times, so they just keep incrementing beyond their intended values. Try resetting them to 0, or just use the loop counters instead of extra variables like that.

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

Sidebar

Related Questions

So I had another question on here of how I could find all the
This is my first question here on Stack Overflow. I generally find all my
Further to the question posted here: Can you find all classes in a package
I couldn't find this question on here: Is there a way to create a
Fairly straightforward question here, but I can't seem to find someone who has asked
Maybe is a often repeated question here, but i can't find anything similar with
I have a quick question here. I know that the cakePHP find('first') function returns
Right, simple question here. I couldn't find an answer using google and looking here.
I've looked around here for similar question but couldn't find any that solved my
I cannot find EntityConfiguration. The same question for RC was here but I thought

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.