473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Solving right triangle w/Pythagorean theorem

11
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 15022
weaknessforcats
9,208 Expert Mod 8TB
What is your question?

Have you tried this code?
Oct 27 '07 #2
inferi9
11
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 Expert 2GB
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
i waited and it stopped but it gave me a very crazy numbers i want only to execute the side1,side2,hypotenuse. that satisfay the equation (side1)^2 + (side2)^2 = (hypotenuse)^2
Oct 27 '07 #5
inferi9
11
i used the if statment inside and outside the loop and it is no good
Oct 27 '07 #6
JosAH
11,448 Expert 8TB
Have a look at this page and pay special attention to formula #11.

kind regards,

Jos
Oct 27 '07 #7
Ganon11
3,652 Expert 2GB
i waited and it stopped but it gave me a very crazy numbers i want only to execute the side1,side2,hypotenuse. 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
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 Expert 2GB
Show us the code you are currently working with.
Oct 28 '07 #10
inferi9
11
here it is what I am working on:
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;
  8.  
  9.     for (side1=1;side1<=100;side1++)
  10.  
  11.  
  12.     for (side2=1;side2<=100;side2++)
  13.  
  14.  
  15.     for (hypotenuse=1;hypotenuse<=100;hypotenuse++)
  16.  
  17.  
  18.  
  19.     if ((hypotenuse*hypotenuse)==((side1*side1)+(side2*side2)))
  20.  
  21.  
  22.             cout<<side1<<side2<<hypotenuse<<endl;
  23.  
  24.  
  25.  return 0;
  26.  
  27.  
  28.     }



and it givese avery crazy numbers.
Oct 29 '07 #11
Ganon11
3,652 Expert 2GB
The only thing I can see is that you are printing each number right on top of the other, with no spaces in between. So, for example, the best known pythagorean triple, (3, 4, 5), will print as 345. Put some spaces in there too get separate numbers.
Oct 29 '07 #12

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

Similar topics

37
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...
12
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...
16
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...
6
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...
5
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...
6
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...
12
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.