I need help comling this into C++ Visual Studio. Write a program that determines
ID: 3870027 • Letter: I
Question
I need help comling this into C++ Visual Studio.
Write a program that determines if a given triangle is a right triangle or not. The program should read the length of the 3 sides of a triangle. All numbers will be integers. The sides can be entered in any order.
If sum of the squares of the two small sides (called the legs) equals the square of the longest side (called the hypotenuse), then the triangle is a right triangle:
Sample Run #1: (the highlighted text is what the user types)
3 sides? 3 4 5
Longest side is 5
It is a right triangle
Press any key to continue . . .
Sample Run #2: (the highlighted text is what the user types)
3 sides? 5 4 3
Longest side is 5
It is a right triangle
Press any key to continue . . .
Sample Run #3: (the highlighted text is what the user types)
3 sides? 3 3 5
Longest side is 5
It is NOT a right triangle
Press any key to continue . . .
Other Considerations:
Did you remember system(“PAUSE”)?
Did you include comments?
Did you use constants?
Did you use good variable names?
Do you have consistent indentation?
Did you remember system(“PAUSE”)?
Did you include comments?
Did you use constants?
Did you use good variable names?
Do you have consistent indentation?
Explanation / Answer
code:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,flag=1;
cout<<"3 sides?";
cin>>a>>b>>c;
cout<<endl<<endl;
if(a>b) //checking if a>b>c then a is longest side
{
if(a>c)
{
cout<<"Longest side is"<<" "<<a<<endl;
if(a*a==b*b+c*c) // if it is true flag variable is set to 0
{
flag=0;
}
}
}
else{
if(b>c) //if b>a and b>c then b is longest side
{
cout<<"Longest side is"<<" "<<b<<endl;
if(b*b==a*a+c*c) //// if it is true flag variable is set to 0
{
flag=0;
}
}
else{
cout<<"Longest side is"<<" "<<c<<endl;
if(c*c==a*a+b*b) // if it is true flag variable is set to 0
{
flag=0;
}
}
}
if(flag==1)
{
cout<<"It is NOT a right triangle"<<endl;
}
else{
cout<<"It is a right triangle"<<endl;
}
cout<<"Press any key to continue . . .";
}
out put:
3 sides?3 4 5
Longest side is 5
It is a right triangle
Press any key to continue . . .
3 sides?5 12 13
Longest side is 13
It is a right triangle
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.