# "Can these three numbers be a right triangle?"

Page 1 of 1

## 2 Replies - 1993 Views - Last Post: 08 September 2009 - 03:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124569&amp;s=eed84cb88c95e6f189bad0fede82b20f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Yatta!

Reputation: 17
• Posts: 122
• Joined: 06-September 09

# "Can these three numbers be a right triangle?"

Posted 08 September 2009 - 02:52 AM

Made a program that could answer that question, but I was wondering if there was an easier way to do it.

```#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main()
{
int a, b, c;
string str1, str2;

str1 = "The three numbers you entered can make a right triangle";
str2 = "The three numbers you entered can NOT make a right triangle";

cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout << endl;

if (pow(a, 2.0) + pow(b, 2.0) == pow(c, 2.0))
cout << str1 << endl;
else if (pow(a, 2.0) + pow(c, 2.0) == pow(b, 2.0))
cout << str1 << endl;
else if (pow(b, 2.0) + pow(a, 2.0) == pow(c, 2.0))
cout << str1 << endl;
else if (pow(b, 2.0) + pow(c, 2.0) == pow(a, 2.0))
cout << str1 << endl;
else if (pow(c, 2.0) + pow(a, 2.0) == pow(b, 2.0))
cout << str1 << endl;
else if (pow(c, 2.0) + pow(b, 2.0) == pow(a, 2.0))
cout << str1 << endl;
else
cout << str2 << endl;

return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: "Can these three numbers be a right triangle?"

### #2 moopet

• binary decision maker

Reputation: 343
• Posts: 1,189
• Joined: 02-April 09

## Re: "Can these three numbers be a right triangle?"

Posted 08 September 2009 - 03:07 AM

[quote name='Yatta!' date='8 Sep, 2009 - 08:52 AM' post='759825']
Made a program that could answer that question, but I was wondering if there was an easier way to do it.

Well there are a couple of ways you could do it, but reducing your if()s would be a start. You compare the same thing a couple of times, and you don't need to have the code block execute each time, and you could re-arrange things to make it more readable:

```#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main()
{
int a, b, c, a2, b2, c2;

cout << "Enter three numbers: ";
cin >> a >> b >> c;

a2 = a * a;
b2 = b * b;
c2 = c * c;

if ( (a2 + b2 == c2) || (a2 + c2 == b2) || (b2 + c2 == a2) )
{
cout << "The three numbers you entered can make a right triangle";
}
else
{
cout << "The three numbers you entered can NOT make a right triangle";
}
return 0;
}

```

### #3 Yatta!

Reputation: 17
• Posts: 122
• Joined: 06-September 09

## Re: "Can these three numbers be a right triangle?"

Posted 08 September 2009 - 03:09 AM

Ah, I get it. Thanks.