Convert your algorithm into a C++ program. Your program should output the value of the longest side of the right-angled triangle.
Note, to answer the question, you will need to be able to do square root. To do square root, follow these 2 steps.
1) include the cmath library that allows square root using the statement
#include <cmath>
2) To perform a square root, use the syntax
sqrt(a double variable or number goes in here);
For example
cout << sqrt(25.0);
OR
double answer = sqrt(25.0);
The first statement above will display 5 to the output screen and the second statement will store the 5 in a variable. Use whichever you like
C++ code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//initializing two sides
double side1,side2;
//asking for their lengths
cout<<"Enter the lengths of the other two
sides: ";
//accepting them
cin>>side1>>side2;
//printing the length of longest side by finding
square root of both sides squared and added
cout<<"The length of the longest side is
"<<sqrt(side1*side1+side2*side2)<<endl;
return 0;
}
Screenshot:
Input and Output:
Get Answers For Free
Most questions answered within 1 hours.