You can mail me Contact Me!

Right triangle checker program

Right triangle checker program

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


Pseudocode:

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
Program:

# 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.")
Flowchart:
flowchart TD A([Start]) --> B[/Input first side length/] B --> C[/Input second side length/] C --> D[/Input third side length/] D --> E[Call is_right_triangle function] subgraph is_right_triangle[is_right_triangle Function] F[Sort the three sides] --> G{Is largest side² = sum of other sides²?} G --> |Yes| H[Return True] G --> |No| I[Return False] end E --> is_right_triangle H --> J{Is return value True?} I --> J J --> |Yes| K[/Display "The triangle is a right triangle"/] J --> |No| L[/Display "The triangle is not a right triangle"/] K --> M([End]) L --> M

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.

right triangle checker program


3 comments

  1. If statement has no condition in the program
    1. In the provided program, the `if` statement does have a condition.

      ```python
      if is_right_triangle(side1, side2, side3):
      print("The triangle is a right triangle.")
      else:
      print("The triangle is not a right triangle.")
      ```
      - Condition: The `if` statement uses the return value of the function `is_right_triangle(side1, side2, side3)` as its condition.

      - The function `is_right_triangle(a, b, c)` returns a Boolean value (`True` or `False`), which serves as the condition for the `if` statement.

      - `True`: Executes the code inside the `if` block (`print("The triangle is a right triangle.")`).

      - `False`: Executes the code inside the `else` block (`print("The triangle is not a right triangle.")`).

      Thus, the condition here is the result of evaluating the `is_right_triangle` function.
  2. # To input lengths of the three sides of the triangle in a single line use:

    side1, side2, side3 = map(float, input("Enter the lengths of the three sides separated by spaces: ").split())
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.