473,749 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Solving right triangle w/Pythagorean theorem

11 New Member
hi everyone I am new here and I have this C++ program that I have to write but it keep given me nothing useful. here is the question:


A right triangle can have sides that are all integers. A set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the following relationship:
(side1)^2 + (side2)^2 = (hypotenuse)^2
Output all Pythagorean triples for side1, side2, and hypotenuse all no longer 100.

For example:
side1 side2 hypotenuse
3 4 5


you have to do it using nested for only and print the result.

Expand|Select|Wrap|Line Numbers
  1. //A program that cheakes A right triangle 
  2. #include <iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6.     //Declare variables.
  7.     int side1, side2, hypotenuse,i;
  8.  
  9.  
  10.  
  11.     for (i=1;i<=100;i++)
  12.     {
  13.         for (side1=1;side1<=100;side1++)
  14.  
  15.  
  16.  
  17.     for (side2=1;side2<=100;side2++)
  18.  
  19.  
  20.         for (hypotenuse=1;hypotenuse<=100;hypotenuse++)
  21.  
  22.  
  23.  
  24.  
  25.  
  26.         if ((hypotenuse*hypotenuse)==((side1*side1)+(side2*side2)))
  27.  
  28.  
  29.             cout<<side1<<side2<<hypotenuse<<endl;
  30.  
  31.  
  32.     }
  33.  
  34.  
  35.  
  36.  
  37.             return 0;
  38.  
  39.  
  40.     }
Oct 27 '07 #1
11 15056
weaknessforcats
9,208 Recognized Expert Moderator Expert
What is your question?

Have you tried this code?
Oct 27 '07 #2
inferi9
11 New Member
my question is that I have to write a loop for side1 and a loop foe side two and a loop for the hypotenuse. and the three must be for loop. the must stopes when this equation: (side1)^2 + (side2)^2 = (hypotenuse)^2
is found and the print all side1,side2 and hypotenuse. i tried this code but it gives me an infinite loop.
can anyone help?
thank you
Oct 27 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Nothing in that code should give you an infinite loop - it simply takes a very long time to execute. One thing you can do to shorten this time is to remove the outermost loop (the one with i controlling it), as this contributes nothing to your code, but multiplies the executions done by 100. Still, your code is doing 100 * 100 * 100 = 1,000,000 executions, so give it some time before deciding it's an infinite loop.
Oct 27 '07 #4
inferi9
11 New Member
i waited and it stopped but it gave me a very crazy numbers i want only to execute the side1,side2,hyp otenuse. that satisfay the equation (side1)^2 + (side2)^2 = (hypotenuse)^2
Oct 27 '07 #5
inferi9
11 New Member
i used the if statment inside and outside the loop and it is no good
Oct 27 '07 #6
JosAH
11,448 Recognized Expert MVP
Have a look at this page and pay special attention to formula #11.

kind regards,

Jos
Oct 27 '07 #7
Ganon11
3,652 Recognized Expert Specialist
i waited and it stopped but it gave me a very crazy numbers i want only to execute the side1,side2,hyp otenuse. that satisfay the equation (side1)^2 + (side2)^2 = (hypotenuse)^2
I took your code and did 2 things to it:

1) I removed the first for...loop, as it was unnecessary.
2) You typed in side2 with some spaces in the middle, and I removed those spaces so it actually compiled.

After this, your program worked like a charm, giving me all the pythagorean triples I needed.
Oct 28 '07 #8
inferi9
11 New Member
I took your code and did 2 things to it:

1) I removed the first for...loop, as it was unnecessary.
2) You typed in side2 with some spaces in the middle, and I removed those spaces so it actually compiled.

After this, your program worked like a charm, giving me all the pythagorean triples I needed.
I removed the first for loop but I do not really get that there are spaces in side2.
the other thing is do I have to put one of the loops as the first loop and put the others in a btraket { }.
Oct 28 '07 #9
Ganon11
3,652 Recognized Expert Specialist
Show us the code you are currently working with.
Oct 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

37
3403
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function to extract pythagorean triples from an input stream. The input is formatted so that only the first two components <a,b> of a pythagorean triple are specified. The function signature will be: std::istream &operator>>(std::istream &is,...
12
8031
by: deanfamily11 | last post by:
Now, I've made a program that when the user enters 3 numbers, it is supposed to tell the user if it is a right triangle by using the quadratic equation (a^2 + b^2 = c^2). Now, granted it can be difficult to come up with 3 numbers on the fly that when run through that equation come out true, but even with using 0's, it should be correct. If there is something wrong with the following code, I'd appreciate the help, otherwise, tell me some...
16
3273
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to build this "compact matrix" (see later).
6
2035
by: 3than7 | last post by:
I am writing an application to solve Pythagorean Theorum Problems. This is on my own time, i am using a book to learn c++, and after doing a fahrenheit to celsuis program from that book, i wanted to try to make something all be meself. I have it working great to find the hypotenuse, but am having some dufficulty making it produce a missing leg. As you know, a^2 + b^2 = c^2 I have a variable that does the input for the one of the legs...
5
2449
by: Carramba | last post by:
theorem states that: Integer n is prime if and only if (x +1)^n ≡ x^n +1 (mod n) in Z. so I testing it, but values doesn't match ... and I don't se why.. I guess :) it's some thing wrong in my implementation... hope you can help out :) #include <stdlib.h> #include <stdio.h>
6
13366
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a triangle that aligns to the left of the screen, but it loops and disappears immediately. So, my problem is getting it to stay on the screen and then ask the user if they want to create another one AND to make it print the first symbol in the center of...
12
7424
by: abkierstein | last post by:
This is my 1st program and I need some help. I've almost got this one finished but I don't know where to go from here. There is something wrong with the sides I've assigned. Any tips? // Program: Pythagorean Theorem // Written by: Hellbreaker // // #include <iostream> #include <cmath> using namespace std;
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.