Question

In: Computer Science

Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...

Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2

Solutions

Expert Solution

Code win python

def isright(list):
    list.sort() # sorting to find the largest element
    a = list[0]
    b = list[1]
    c = list[2]
    if a < 0 or b < 0 or c < 0:  # checking if any element is less than zero
        return 0
    else:
        if (a * a + b * b) == c * c: # checking a^2 + b^2 = c^2
            return 1
        else:
            return 0


if __name__ == '__main__':
    n = int(input("Enter the number of series of elements:"))
    data = []
    for i in range(n):
        print("Sides for triangle {i}:".format(i=i+1))
        a = int(input())
        b = int(input())
        c = int(input())
        list = [a, b, c]
        data.append(list)      # storing data in list like [[1,2,3],[2,4,5]]
    for sides in data:
        if isright(sides):
            print("{a},{b},{c} is a right angled triangle".format(a=sides[0], b=sides[1], c=sides[2]))
        else:
            print("{a},{b},{c} is not a right angled triangle".format(a=sides[0], b=sides[1], c=sides[2]))

Output:

Explaination:

Initially take the number of series of elements which the user wants to input which is stored in n. For each series we form a list/array of elements which we store in a list so the data variable has something like this:

data = [[1,2,3], [3,4,5], [5,7,9] and so on]

for each array in data i.e for [1, 2, 3] (initial case according to the example)

then in the function isright we first sort the list of elements i.e if we get say list as [10, 6,9] we need to find the largest of three element which will be our c then we checik the equation a^2 + b^2 = c^2 if it is true or not then return 0 or 1 accordingly.

Screenshot of code for getting the spaces in python:


Related Solutions

Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 Codes in C please.s
(JAVA) Write an application that reads three nonzero values entered by the user and determines and...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Enter three sizes, separated by spaces(decimals values are acceptable): 4.5·5.5·3.5 A triangle could measure 4.50, 5.50, by 3.50.
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...
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
Write an application that reads three integers, adds all three together and computes an average of...
Write an application that reads three integers, adds all three together and computes an average of the three entries and computes any remainder of the integer division. A remainder represents the modulus result of dividing an even by an odd number or vice versa. Display the output. Enter an integer score 3 Enter an integer score 6 Enter an integer score 4 The average of 3, 6, 4 is 4 with a remainder of 1 Press any key to continue...
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and...
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and does the following  Plays round number of rounds, rolling two s-sided dice per round (use roll).  Uses the previous functions to calculate the score of each round  Prints out the dice rolls and the score for each round  Accumulates the score over all rounds  Returns the final score over all rounds. *Write a function result that takes an arguments...
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...
Argument A Since all triangles have three sides, and an isosceles triangle is a triangle, it...
Argument A Since all triangles have three sides, and an isosceles triangle is a triangle, it follows that an isosceles triangle has three sides. Argument a is: inductive/deductive; valid/invalid/strong/weak; sound/unsound/cogent/uncogent? Argument B There is a large crack in Philadelphia's Liberty Bell. From this, we can conclude that there was a defect in the bell's craftsmanship. Argument B is: inductive/deductive; valid/invalid/strong/weak; sound/unsound/cogent/uncogent? Argument C Paleontologists now agree that the Tyrannosaurus Rex was actually a peaceful, plant-eating dinosaur. Therefore, it is probably...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT