Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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)<sup>2</sup> = (perpendicular)<sup>2</sup> + (Base)<sup>2</sup> Or H = ?(l)<sup>2</sup> + (b)<sup>2</sup>
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.
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation <span class="token keyword">import</span> Glibc <span class="token keyword">var</span> triPerpendicular <span class="token operator">=</span> <span class="token number">8.0</span> <span class="token keyword">var</span> triBase <span class="token operator">=</span> <span class="token number">10.0</span> <span class="token comment">// Finding the hypotenuse of the right-angled triangle </span> <span class="token keyword">var</span> triHypotenuse <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token function">pow</span><span class="token punctuation">(</span>triPerpendicular<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">pow</span><span class="token punctuation">(</span>triBase<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Perpendicular - "</span><span class="token punctuation">,</span> triPerpendicular<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Base - "</span><span class="token punctuation">,</span> triBase<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hypotenuse - "</span><span class="token punctuation">,</span> triHypotenuse<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
<div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation <span class="token keyword">import</span> Glibc <span class="token comment">// Taking input from the user </span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the sides of right-angled triangle"</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Enter the value of Perpendicular:"</span><span class="token punctuation">)</span> <span class="token keyword">var</span> triPerpendicular <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Enter the value of base:"</span><span class="token punctuation">)</span> <span class="token keyword">var</span> triBase <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span> <span class="token comment">// Finding the hypotenuse of the right-angled triangle </span> <span class="token keyword">var</span> triHypotenuse <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token function">pow</span><span class="token punctuation">(</span>triPerpendicular<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">pow</span><span class="token punctuation">(</span>triBase<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Perpendicular - "</span><span class="token punctuation">,</span> triPerpendicular<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Base - "</span><span class="token punctuation">,</span> triBase<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hypotenuse - "</span><span class="token punctuation">,</span> triHypotenuse<span class="token punctuation">)</span> </div>
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.
