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. - //A program that cheakes A right triangle
-
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
//Declare variables.
-
int side1, side2, hypotenuse,i;
-
-
-
-
for (i=1;i<=100;i++)
-
{
-
for (side1=1;side1<=100;side1++)
-
-
-
-
for (side2=1;side2<=100;side2++)
-
-
-
for (hypotenuse=1;hypotenuse<=100;hypotenuse++)
-
-
-
-
-
-
if ((hypotenuse*hypotenuse)==((side1*side1)+(side2*side2)))
-
-
-
cout<<side1<<side2<<hypotenuse<<endl;
-
-
-
}
-
-
-
-
-
return 0;
-
-
-
}
11 15022
What is your question?
Have you tried this code?
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
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.
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 used the if statment inside and outside the loop and it is no good
Have a look at this page and pay special attention to formula #11.
kind regards,
Jos
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.
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 { }.
Show us the code you are currently working with.
here it is what I am working on: - //A program that cheakes A right triangle
-
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
//Declare variables.
-
int side1, side2, hypotenuse;
-
-
for (side1=1;side1<=100;side1++)
-
-
-
for (side2=1;side2<=100;side2++)
-
-
-
for (hypotenuse=1;hypotenuse<=100;hypotenuse++)
-
-
-
-
if ((hypotenuse*hypotenuse)==((side1*side1)+(side2*side2)))
-
-
-
cout<<side1<<side2<<hypotenuse<<endl;
-
-
-
return 0;
-
-
-
}
and it givese avery crazy numbers.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |