Pythagoras theorem

EasyBasic Programs · 9 of 40

Write a Python program to find the hypotenuse of a right-angled triangle using the Pythagoras theorem: a² + b² = c².

Advertisement
Input
a = 3, b = 4
Output
Hypotenuse c = 5.0
Check: True

Approach

  1. Compute a² + b².
  2. Take the square root to get c.

Solution

Python
import math a = 3b = 4c = math.sqrt(a ** 2 + b ** 2)print("Hypotenuse c =", c)print("Check:", a ** 2 + b ** 2 == c ** 2)
Output
Hypotenuse c = 5.0
Check: True

Line by line

Line 1

math provides sqrt().

Line 5

The hypotenuse is the square root of the sum of the squares of the other two sides.

Line 7

Squaring c again confirms the theorem holds.

Tip

math.hypot(a, b) computes the same value in one call.

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs

Leave a Comment