Swift Program to find the hypotenuse of a right-angled triangle with sides l and b


This tutorial will discuss how to write a Swift program to find the hypotenuse of a right-angled triangle with sides l and b.

A triangle in which one angle is equal to 90 degrees and the sum of the other two angles is also 90 degrees then such type of triangle is known as a right-angled triangle. A right-angled triangle has three sides- base, perpendicular, and hypotenuse. We can find the relationship between these three sides using Pythagoras theorem.

In a right-angled triangle, the square of the hypotenuse side(also known as the largest side of the triangle) is equal to the sum of the squares of the other two sides is known as the Pythagoras theorem.

Formula

Following is the formula of the hypotenuse −

(hypotenuse)2 = (perpendicular)2 + (Base)2
Or
H = √(l)2 + (b)2

Below is a demonstration of the same −

Input

Suppose our given input is −

l = 6 
b = 8

Output

The desired output would be −

H = 10

Algorithm

Following is the algorithm −

  • Step 1 − Declare two variables named “triPerpendicular” and “triBase” with user-defined/pre-defined values.

  • Step 2 − Declare another variable named “triHypotenuse”. It stores the value of hypotenuse that is derived from the following formula

var triHypotenuse = Int(sqrt(pow(triPerpendicular, 2) + pow(triBase, 2)))
  • Step 3 − Print the output

Example 1

The following program shows how to find the hypotenuse of a right-angled triangle with sides l and b.

import Foundation import Glibc var triPerpendicular = 8.0 var triBase = 10.0 // Finding the hypotenuse of the right-angled triangle var triHypotenuse = Int(sqrt(pow(triPerpendicular, 2) + pow(triBase, 2))) print("Perpendicular - ", triPerpendicular) print("Base - ", triBase) print("Hypotenuse - ", triHypotenuse)

Output

Perpendicular -  8.0
Base -  10.0
Hypotenuse -  12

Here, in the above code, we find the hypotenuse of the right angled triangle using the Pythagoras theorem −

var triHypotenuse = Int(sqrt(pow(triPerpendicular, 2) + pow(triBase, 2)))

Here we find the power of perpendicular and base using pow() function, then find the square root of the sum of perpendicular and base using sqrt() function and convert the floating point result into integer using Int() function. Given perpendicular = 8.0 and Base = 10.0 so the hypotenuse = 12.

Example 2

The following program shows how to find the hypotenuse of a right-angled triangle with sides l and b.

import Foundation import Glibc // Taking input from the user print("Please enter the sides of right-angled triangle") print("Enter the value of Perpendicular:") var triPerpendicular = Double(readLine()!)! print("Enter the value of base:") var triBase = Double(readLine()!)! // Finding the hypotenuse of the right-angled triangle var triHypotenuse = Int(sqrt(pow(triPerpendicular, 2) + pow(triBase, 2))) print("Perpendicular - ", triPerpendicular) print("Base - ", triBase) print("Hypotenuse - ", triHypotenuse)

STDIN Input

Please enter the sides of right-angled triangle 
Enter the value of Perpendicular: 6 
Enter the value of base: 8

Output

Perpendicular - 6.0 
Base - 8.0 
Hypotenuse - 10

Here, in the above code, we find the hypotenuse of the right angled triangle using the Pythagoras theorem−

var triHypotenuse = Int(sqrt(pow(triPerpendicular, 2) + pow(triBase, 2)))

Here we find the power of perpendicular and base using pow() function, then find the square root of the sum of perpendicular and base using sqrt() function and convert the floating point result into integer using Int() function. Here, the value of perpendicular and base is given by the user. So the user input the value of perpendicular = 6.0 and Base = 8.0 so the hypotenuse = 10.

Updated on: 25-Aug-2022

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements