2 Replies - 1968 Views - Last Post: 08 September 2009 - 03:09 AM Rate Topic: -----

#1 Yatta!  Icon User is offline

  • D.I.C Head

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

"Can these three numbers be a right triangle?"

Post icon  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  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • 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;
}


Was This Post Helpful? 1
  • +
  • -

#3 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1