A program that accepts the lengths of three sides of a triangle as inputs. The program should output 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). Implement using functions.
Algorithm:
1. Start
2. Define function is_right_triangle that takes three parameters a, b, c
a. Create a sorted list of the three sides
b. Check if square of largest side equals sum of squares of other two sides
c. Return true if equation holds, false otherwise
3. Input first side length (side1)
4. Input second side length (side2)
5. Input third side length (side3)
6. Call is_right_triangle function with side1, side2, side3
7. If function returns true
- Display "The triangle is a right triangle"
8. Else
- Display "The triangle is not a right triangle"
9. End
Hello, dear reader! 👋
Thanks for visiting my blog! I’m a student just like you, sharing what I learn to help others with Python programming. I hope my posts are useful for your studies! 😊
If you find this post helpful, please leave a comment—even just a few emojis will make my day! 🐍✨ Your feedback keeps me motivated to create more content for everyone. 🚀
Happy programming!
— Abhin Krishna, S01, EB Department, MEC
FUNCTION is_right_triangle(a, b, c)
sides ← SORT([a, b, c]) // Sort in ascending order
IF sides[2]² = sides[0]² + sides[1]² THEN
RETURN true
ELSE
RETURN false
END IF
END FUNCTION
BEGIN
PRINT "Enter the length of the first side: "
INPUT side1
PRINT "Enter the length of the second side: "
INPUT side2
PRINT "Enter the length of the third side: "
INPUT side3
IF is_right_triangle(side1, side2, side3) THEN
PRINT "The triangle is a right triangle."
ELSE
PRINT "The triangle is not a right triangle."
END IF
END
# Function to check if the given sides form a right triangle
def is_right_triangle(a, b, c):
# Sort the sides to ensure that c is the largest side
sides = sorted([a, b, c])
# Apply the Pythagorean Theorem
return sides[2] ** 2 == sides[0] ** 2 + sides[1] ** 2
# Input lengths of the three sides of the triangle
side1 = float(input("Enter the length of the first side: "))
side2 = float(input("Enter the length of the second side: "))
side3 = float(input("Enter the length of the third side: "))
# Check if the sides form a right triangle
if is_right_triangle(side1, side2, side3):
print("The triangle is a right triangle.")
else:
print("The triangle is not a right triangle.")
Important!
If you find any mistakes in my code or flowchart, please comment below this post. I will be happy to correct them and clear up any doubts you may have.