Swift Program to calculate the area of the rhombus

This tutorial will discuss how to write a Swift program to calculate the area of the rhombus.

A rhombus is a closed two-dimensional figure or we can say that a rhombus is a quadrilateral whose all the sides are equal and the diagonals intersect each other at a right angle(that is 90 degrees).

The area of the rhombus is known as the total space enclosed inside the boundaries of the rhombus. We can calculate the area of the rhombus using any of the following methods ?

  • Area of rhombus using diagonal

  • Area of rhombus using base and height

  • Area of rhombus using trigonometry

Area of rhombus using diagonal

We can calculate the area of the rhombus using diagonals. The area is equal to half of the product of the length of both diagonals.

Formula

Following is the formula of the area of the rhombus ?

Area = 1/2 x (diagonal1) x (diagonal2)

Algorithm

Following is the algorithm ?

  • Step 1 ? Declare two variables to store the value of height and base of the rhombus. Here the height and base of the rhombus can be user-defined or pre-defined.

  • Step 2 ? Declare another variable named as "areaRhombus" to store the area of the rhombus.

var areaRhombus = (rDiagonal1 * rDiagonal2) / 2
  • Step 3 ? Print the output.

Example

The following program shows how to calculate the area of the rhombus.

<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> rDiagonal1 <span class="token operator">=</span> <span class="token number">6</span>
<span class="token keyword">var</span> rDiagonal2 <span class="token operator">=</span> <span class="token number">8</span>

<span class="token comment">// Finding area of rhombus</span>
<span class="token keyword">var</span> areaRhombus <span class="token operator">=</span> <span class="token punctuation">(</span>rDiagonal1 <span class="token operator">*</span> rDiagonal2<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Diagonal 1 - "</span><span class="token punctuation">,</span> rDiagonal1<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Diagonal 2 - "</span><span class="token punctuation">,</span> rDiagonal2<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the area of Rhombus is"</span><span class="token punctuation">,</span> areaRhombus<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Diagonal 1 -  6
Diagonal 2 -  8
Hence the area of Rhombus is 24

Here, in the above code, we find the area of the rhombus by finding the half of the product of its diagonals using the following code ?

var areaRhombus = (rDiagonal1 * rDiagonal2) / 2

Here, rDiagonal1 = 6 and rDiagonal2 = 8, so the area of the rhombus 24((6 * 8)/2 = 24)

Area of rhombus using base and height

We can also find the area of the rhombus by multiplying the base and height of the rhombus.

Formula:

Following is the formula of the area of the rhombus ?

Area = (base) x (height)

Algorithm

Following is the algorithm ?

  • Step 1 ? Declare two variables to store the value of height and base of the rhombus. Here the height and base of the rhombus can be user-defined or pre-defined.

  • Step 2 ? Declare another variable named as "areaRhombus" to store the area of the rhombus.

var areaRhombus = rSideLength * rHeight
  • Step 3 ? Print the output.

Example

The following program shows how to calculate the area of the rhombus using base and 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> rSideLength <span class="token operator">=</span> <span class="token number">10</span> 
<span class="token keyword">var</span> rHeight <span class="token operator">=</span> <span class="token number">7</span>

<span class="token comment">// Finding the area of rhombus </span>
<span class="token keyword">var</span> areaRhombus <span class="token operator">=</span> rSideLength <span class="token operator">*</span> rHeight

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Side length - "</span><span class="token punctuation">,</span> rSideLength<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height - "</span><span class="token punctuation">,</span> rHeight<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the area of Rhombus is"</span><span class="token punctuation">,</span> areaRhombus<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Side length - 10 
Height - 7 
Hence the area of Rhombus is 70

Here, in the above code, we find the area of the rhombus by multiplying the base and height of the rhombus using the following code ?

var areaRhombus = rSideLength * rHeight

Here, rSideLength = 10 and Height = 7 so the area of the rhombus is 70(10 * 7 = 70).

Area of rhombus using trigonometry

We can calculate the area of rhombus when the sides and the angle is known with the help of trigonometry. Here we find the area by multiplying the square of side with the sin value of the given angle.

Formula

Following is the formula of the area of the rhombus ?

Area = (base)<sup>2</sup> x sin(theta)

Algorithm

Following is the algorithm ?

  • Step 1 ? Declare a variable to store the side of the rhombus. Here the value can be user-defined or pre-defined.

  • Step 2 ? Find the radian value of the angle and store the result in rTheta variable.

var rTheta = 90 * (Double.pi / 180)
  • Step 3 ? Find the area of rhombus and store the result into "areaRhombus" variable.

var areaRhombus = pow(rSideLength, 2) * sin(rTheta)
  • Step 4 ? Print the output.

Example

The following program shows how to calculate the area of the rhombus using trigonometry.

<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> rSideLength <span class="token operator">=</span> <span class="token number">10.0</span>

<span class="token comment">// Finding the radian value of the angle </span>
<span class="token keyword">var</span> rTheta <span class="token operator">=</span> <span class="token number">90</span> <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</span> <span class="token punctuation">)</span>

<span class="token comment">// Finding the area of rhombus </span>
<span class="token keyword">var</span> areaRhombus <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span>rSideLength<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">sin</span><span class="token punctuation">(</span>rTheta<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Side length - "</span><span class="token punctuation">,</span> rSideLength<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Angle(in radian)-"</span><span class="token punctuation">,</span> rTheta<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the area of Rhombus is"</span><span class="token punctuation">,</span> areaRhombus<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Side length - 10.0 Angle(in radian)- 1.5707963267948966 
Hence the area of Rhombus is 100.0

Here in the above code, first we find the radian value of the given angle and then we calculate the area of the rhombus using the following code ?

var areaRhombus = pow(rSideLength, 2) * sin(rTheta)

Here we use pow() function to calculate the power of side and sin() function is used to calculate the sin value of the given angle. The given side = 10 and angle = 1 so the area of the rhombus is 100.

Updated on: 2022-08-25T08:08:24+05:30

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements