Question

In: Computer Science

Write a program that prompts for the lengths of the sides of a triangle and reports...

Write a program that prompts for the lengths of the sides of a triangle and reports the three angles. Make sure you have a good introduction stating what the program does when it is run, and a helpful prompt for the user when asking for input:

i.e. Here is the generated output from a sample program:

This program computes the angles of a triangle given the lengths of the sides.

What is the length of side 1? <wait for user input>

What is the length of side 2? <wait for user input>

What is the length of side 3? <wait for user input>

angle 1 = <angle in degrees for side 1, side 2, and side 3>

angle 2 = <angle in degrees for side 2, side 3, and side 1>

angle 3 = <angle in degrees for side 3, side 1, and side 2>

Requirements

  • Your program must have at least 1 function with parameters, that when called with arguements returns a value.

Hints

  • Make sure to store your input in a variable.
  • Convert your input from a string to a float.
  • You will need to import the math library
  • You can use a variant of the law of cosines to compute the angles:
    • angle = arccos( (a^2 + b^2 - c^2) / (2ab) )
  • The math.acos() and math.degrees() methods will be useful.
  • Write a function for computing the angle that takes three parameters, a, b, and c
  • You only need one function for computing all three angles.

Save your program as triangle_angles.py and attach it.

Solutions

Expert Solution

For the given triangle,

When three sides are given, angles can be calculated using cosine law.

In the output please take care that the angle 1 is the angle opposite to side 1, angle 2 is the angle opposite to side 2,

The code to do so is:

The sample input and output for this code is:

**The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts, please comment i will surely help. Please take care of the indentation while copying the code. Check from the screenshot provided. **

**Code: **

import math
def findAngles(s1, s2, s3):
#"angles" is the list to store the angles of the triangle
angles=[]

#Calculate the angles using the cosine law
#To get the angles in degrees, use math.degrees() function
a1 = math.degrees(math.acos((s2**2 + s3**2 - s1**2)/(2*s2*s3)))
a2 = math.degrees(math.acos((s1**2 + s3**2 - s2**2)/(2*s1*s3)))
a3 = math.degrees(math.acos((s1**2 + s2**2 - s3**2)/(2*s1*s2)))

#Storing the angles in the list "angles"
angles.append(a1)
angles.append(a2)
angles.append(a3)
return angles

print("This program is used to find the angles of a triangle when its three sides are given")
print("So Enter the three sides of a triangle")
# Take the input data
s1 = float(input("Enter the first side: "))
s2 = float(input("Enter the second side: "))
s3 = float(input("Enter the third side: "))
# Function call to find and return angles in the form of a list
angles = findAngles(s1, s2, s3)

print("Angles in degrees: ")
for x in range(len(angles)):
print(f"Angle{x+1} = ", angles[x])


Related Solutions

Write a program that accepts the lengths of three sides of a triangle as inputs. The...
Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides. Use The triangle is a right triangle. and The triangle is not a right triangle. as your final outputs. An example of the program input...
(PYTHON) Write aprogram that prompts user to enter three sides of a triangle....
(PYTHON) Write a program that prompts user to enter three sides of a triangle. The program should determine if the three sides can form a triangle. If the three sides can form a triangle, then determine the type of the triangle.There are three types of triangles: Equilateral triangle (all 3 sides are equal) Isosceles triangle (two sides are equal, the third side is of a different length) Scalene triangle (all 3 sides are of different lengths)   The program should...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a triangle as input . The program output should indicate if the triangle is a right triangle, an acute triangle, or an obtuse triangle. Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Write a program that prompts for and reads in the two side lengths of a right...
Write a program that prompts for and reads in the two side lengths of a right triangle (floating point numbers) and then calls a float-valued function, hypot1, that calculates and returns the length of the hypotenuse of the triangle. The program then displays the two side lengths and the hypotenuse length. Note: The hypot1 function returns the hypotenuse length – it does not display it; the function should not contain any cout nor cin statements. Math review: Recall that for...
Write a program that prompts the user for the length of one side of a triangle...
Write a program that prompts the user for the length of one side of a triangle and the sizes of the two adjacent angles in degrees and then displays the length of the two other sides and the size of the third angle.
Assignment: Write an interactive C++·program to determine a type of a triangle based on three sides....
Assignment: Write an interactive C++·program to determine a type of a triangle based on three sides. Have a user input the length of three sides of a triangle. Allow the user to enter the sides in any order. Determine if the entered sides form a valid triangle. The triangle may not have negative sides. The sum of any two sides must be greater than the third side to form a triangle. Determine if the entered sides are part of a...
An equilateral triangle is a triangle whose sides are equal. You are to write a class...
An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39. Details: 1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT