Swift Program to Find the Area of a Parallelogram

This tutorial will discuss how to write a swift program to find the area of a parallelogram.A Parallelogram is a quadrilateral that is created using parallel lines.

Where the opposite sides are parallel and equal and the opposite angles of the parallelograms are equal. It is of three types - rectangle, square, and rhombus.

In a parallelogram, an area is known as the space that is enclosed inside the boundaries or sides of the parallelogram in the two-dimensional plane. Suppose we have a tea try now the area of the parallelogram helps us to find how much paint we required to cover the top of the tea tray. We can calculate the area of the parallelogram using the following ways ?

Using Height

Using the length of the sides

Using diagonals

Area of a parallelogram using height

We can calculate the area of the parallelogram with the help of height. Suppose we have P and Q as the set of the parallel sides and the distance between them is H. H is also known as the height of the parallelogram. So the area of the parallelogram is the product of the base and the height.

Formula

Following is the formula of the area of a parallelogram

Area = Base x Height<br>Or<br>Area = Q x H

Where Q is the base and H is the height of the parallelogram.

Algorithm to find Area of a Parallelogram using Height

  • Step 1 ? Define two variables for Base and Height

  • Step 2 ? Assign the value of those variables

  • Step 3 ? Implement Area of Parallelogram formula using height (Area = Base x Height)

  • Step 4 ? Print the output

Example

The following program shows how to calculate the area of the parallelogram with the help of height.

<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> base <span class="token operator">=</span> <span class="token number">12</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> AreaOfParallelogram <span class="token operator">=</span> base <span class="token operator">*</span> height

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Base of the parallelogram:"</span><span class="token punctuation">,</span> base<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height of the parallelogram:"</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 parallelogram: "</span><span class="token punctuation">,</span> AreaOfParallelogram<span class="token punctuation">)</span></div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Base of the parallelogram: 12
Height of the parallelogram: 10
Final area of the parallelogram: 120

In the above code, we find the area of a parallelogram using the mathematical formula as shown in the below code ?

var AreaOfParallelogram = base * height

Here the base of the parallelogram is 12 and the height of the parallelogram is 10. So the area of the parallelogram is 120.

Area of a parallelogram using sides

We can calculate the area of the parallelogram with the help of its adjacent sides and the angle between them. Suppose we have P and Q as the adjacent sides and O as the angle (?) between them in Radian. So the area of the parallelogram is the product of both the sides and the angle.

Formula

Following is the formula of the area of a parallelogram

Area = Side1 x Side2 x sin(?)<br>Or<br>Area = P x Q x sin(?)

Where P and Q are the lengths of the sides and O is the angle between them.

Algorithm to find Area of a Parallelogram using Sides

  • Step 1 ? Define three variables (Side 1, Side 2 and Angle)

  • Step 2 ? Assign the value of those variables

  • Step 3 ? Implement Area of Parallelogram formula using Sides (Area = Side1 x Side2 x sin(?))

  • Step 4 ? Print the output

Example

The following program shows how to calculate the area of the parallelogram with the help of sides.

<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.0</span>
<span class="token keyword">var</span> side2 <span class="token operator">=</span> <span class="token number">10.0</span>
<span class="token keyword">var</span> angle <span class="token operator">=</span> <span class="token number">45.0</span>

<span class="token keyword">var</span> AreaOfParallelogram <span class="token operator">=</span> side1 <span class="token operator">*</span> side2 <span class="token operator">*</span> <span class="token function">sin</span><span class="token punctuation">(</span>angle <span class="token operator">*</span> <span class="token punctuation">(</span>Double<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">180.0</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">"Side 1 of the parallelogram:"</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 the parallelogram:"</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">"Angle of the parallelogram:"</span><span class="token punctuation">,</span> angle<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final area of the parallelogram: "</span><span class="token punctuation">,</span> AreaOfParallelogram<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 the parallelogram: 12.0
Side 2 of the parallelogram: 10.0
Angle of the parallelogram: 45.0
Final area of the parallelogram: 84.85281374238569

In the above code, we find the area of a parallelogram using the mathematical formula as shown in the below code ?

var AreaOfParallelogram = side1 * side2 * sin(angle * (Double.pi / 180.0))

Here, the two sides of the parallelogram are 12 and 10 and the angle is 45 degrees. So the area of the parallelogram is 84.85281374238569. In the code, we convert degree into radian using the below formula.

Radian = degrees *(Double.pi / 180.0)

Area of a parallelogram using diagonal

We can calculate the area of the parallelogram with the help of the diagonals. A parallelogram has two diagonals and they both intersect each other at some angle. Suppose we have two diagonals D1 and D2 and O is the angle between them in Radian. So the area of the parallelogram is the product of both the length of the diagonal and their angle.

Formula

Following is the formula of the area of a parallelogram ?

Area = (Diagonal1 x Diagonal2 x sin(?))/2<br>Or<br>Area = (D1 x D2 x sin(O))/2

Where D1 and D2 are the lengths of the sides and O is the angle between them.

Algorithm to find Area of a Parallelogram using Diagonals

  • Step 1 ? Define three variables (Diagonal 1, Diagonal 2 and Angle)

  • Step 2 ? Assign the value of those variables

  • Step 3 ? Implement Area of Parallelogram formula using Diagonal (Area = (Diagonal1 x Diagonal2 x sin(?))/2)

  • Step 4 ? Print the output

Example

The following program shows how to calculate the area of the parallelogram with the help of diagonals.

<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> diagonal1 <span class="token operator">=</span> <span class="token number">35.0</span>
<span class="token keyword">var</span> diagonal2 <span class="token operator">=</span> <span class="token number">40.0</span>
<span class="token keyword">var</span> angle <span class="token operator">=</span> <span class="token number">30.0</span>
<span class="token keyword">var</span> AreaOfParallelogram <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">/</span><span class="token number">2</span> <span class="token operator">*</span> diagonal1 <span class="token operator">*</span> diagonal2 <span class="token operator">*</span> <span class="token function">sin</span><span class="token punctuation">(</span>angle <span class="token operator">*</span> <span class="token punctuation">(</span>Double<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">180.0</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">"Diagonal 1 of the parallelogram:"</span><span class="token punctuation">,</span> diagonal1<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Diagonal 2 of the parallelogram:"</span><span class="token punctuation">,</span> diagonal2<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Angle:"</span><span class="token punctuation">,</span> angle<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final area of the parallelogram: "</span><span class="token punctuation">,</span> AreaOfParallelogram<span class="token punctuation">)</span></div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Diagonal 1 of the parallelogram: 35.0
Diagonal 2 of the parallelogram: 40.0
Angle: 30.0
Final area of the parallelogram: 349.99999999999994

In the above code, we find the area of a parallelogram using the mathematical formula as shown in the below code ?

var AreaOfParallelogram = 1/2 * diagonal1 * diagonal2 * sin(angle * (Double.pi / 180.0))

Here, the two diagonals of the parallelogram are 35 and 40 and the angle is 30 degrees. So the area of the parallelogram is 349.99999999999994. In the code, we convert degree into radian using the below formula.

Radian = degrees *(Double.pi / 180.0)
Updated on: 2022-08-02T14:55:14+05:30

727 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements