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 Area of a Trapezium
This tutorial will discuss how to write a swift program to find the area of a trapezium.
A trapezium is a quadrilateral with at least a pair of opposite parallel sides. The parallel sides are known as the base and the non-parallel sides are known as the legs of the trapezium. The distance between the parallel sides is known as the height of the trapezium.

In a trapezium, an area is known as the space that is enclosed inside the boundaries of the trapezium in the two-dimensional plane. Suppose we have a board, now the area of the trapezium helps us to find how much paint we required to cover the top of the board. We can calculate the area of the trapezium with the help of the parallel sides and the distance between them.
Algorithm to find Area of a Trapezium
Step 1 ? Define two variables
Step 2 ? Enter the value of those variables
Step 3 ? Perform Area of Trapezium formula for those two variables
Step 4 ? Print the output
Formula
Following is the formula of the area of a trapezium ?
Area = ((X + Y)/2)H
Where,
X and Y represent the length of the parallel sides or bases of the trapezium as shown in the above diagram
H represents the height between the parallel sides
Example 1
The following program shows how to calculate the area of a trapezium.
<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> Side1 <span class="token operator">=</span> <span class="token number">12</span> <span class="token keyword">var</span> Side2 <span class="token operator">=</span> <span class="token number">20</span> <span class="token keyword">var</span> Height <span class="token operator">=</span> <span class="token number">10</span> <span class="token keyword">var</span> AreaOfTrapezium <span class="token operator">=</span> <span class="token punctuation">(</span>Side1 <span class="token operator">+</span> Side2<span class="token punctuation">)</span><span class="token operator">*</span><span class="token punctuation">(</span>Height <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Side 1 of Trapezium:"</span><span class="token punctuation">,</span> Side1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Side 2 of Trapezium:"</span><span class="token punctuation">,</span> Side2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height between sides:"</span><span class="token punctuation">,</span> Height<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final area of the Trapezium: "</span><span class="token punctuation">,</span> AreaOfTrapezium<span class="token punctuation">)</span></div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Side 1 of Trapezium: 12 Side 2 of Trapezium: 20 Height between sides: 10 Final area of the Trapezium: 160
In the above code, we find the area of the trapezium using the mathematical formula as shown in the below code ?
var AreaOfTrapezium = (Side1 + Side2)*(Height / 2)
Here, the two sides of the trapezium are 12 and 20 and the height of the trapezium is 19 so the area of the trapezium is 160.
Example 2
The following program shows how to calculate the area of the trapezium with user-defined input.
<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 function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter side 1-"</span><span class="token punctuation">)</span> <span class="token keyword">var</span> TrapSide1 <span class="token operator">=</span> <span class="token function">Int</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">"Please enter side 2-"</span><span class="token punctuation">)</span> <span class="token keyword">var</span> TrapSide2 <span class="token operator">=</span> <span class="token function">Int</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">"Please enter height between the two sides"</span><span class="token punctuation">)</span> <span class="token keyword">var</span> TrapHeight <span class="token operator">=</span> <span class="token function">Int</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 keyword">var</span> AreaOfTrapezium <span class="token operator">=</span> <span class="token punctuation">(</span>TrapHeight<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>TrapSide1 <span class="token operator">+</span> TrapSide2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Side 1:"</span><span class="token punctuation">,</span> TrapSide1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Side 2:"</span><span class="token punctuation">,</span> TrapSide2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Height:"</span><span class="token punctuation">,</span> TrapHeight<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final area of the Trapezium: "</span><span class="token punctuation">,</span> AreaOfTrapezium<span class="token punctuation">)</span></div>
STDIN Input
Please enter side 1- 18 Please enter side 2- 14 Please enter height between the two sides 8
Output
Entered Side 1: 18 Entered Side 2: 14 Entered Height: 8 Final area of the Trapezium: 128
In the above code, we find the area of a trapezium using the mathematical formula as shown in the below code ?
var AreaOfTrapezium = (TrapHeight/2) * (TrapSide1 + TrapSide2)
Here, the height and two sides of the trapezium are given by the user at run time and using these values the area of the trapezium is calculated which is 128.
