Swift Program to Find the Perimeter of a Rectangle

This tutorial will discuss how to write a swift program to find the perimeter of a Rectangle.

A rectangle is a 2-dimensional closed shape with four sides in which opposite sides are parallel and equal to each other. It also has four angle with 90 degrees each.

The perimeter of the rectangle is used to calculate the boundary of the rectangle. Or we can say that it is the total distance covered by the rectangle?s outer boundary as show in the above diagram. It is the one of the most important formula of the rectangle. Suppose we want to fence our rectangular garden so with the help of perimeter we can calculate the total amount of fencing is required to cover the boundary of the garden. As we know that in rectangle the opposite sides of the rectangle are equal so the perimeter of the rectangle is the twice of its length plus twice of its width.

Formula

Following is the formula of the perimeter of a rectangle ?

Perimeter = 2(length + width)

Algorithm to find Perimeter of a Rectangle

  • Step 1 ? Define two variables

  • Step 2 ? Enter the value of those variables

  • Step 3 ? Perform Perimeter of a Rectangle formula for those two variables

  • Step 4 ? Print the output

Example 1

The following program shows how to calculate the perimeter of the rectangle.

<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> RectangleLength <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">var</span> RectangleWidth <span class="token operator">=</span> <span class="token number">5</span>

<span class="token keyword">var</span> perimeterOfRectangle <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>RectangleLength <span class="token operator">+</span> RectangleWidth<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Length of the rectangle:"</span><span class="token punctuation">,</span> RectangleLength<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Width of the rectangle:"</span><span class="token punctuation">,</span> RectangleWidth<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final perimeter of the rectangle: "</span><span class="token punctuation">,</span> perimeterOfRectangle<span class="token punctuation">)</span></div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Length of the rectangle: 10
Width of the rectangle: 5
Final perimeter of the rectangle: 30

In the above code, we find the perimeter of the rectangle using the mathematical formula as shown in the below code ?

var perimeterOfRectangle = 2 * (RectangleLength + RectangleWidth)

Here, the length of the rectangle is 10 and width of the rectangle is 5 so the perimeter of the rectangle is 30.

Example 2

The following program shows how to calculate the perimeter of the rectangle 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 the length-"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> RectLength <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 the width-"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> RectWidth <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> perimeterOfRectangle <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>RectLength <span class="token operator">+</span> RectWidth<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Length:"</span><span class="token punctuation">,</span> RectLength<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Width:"</span><span class="token punctuation">,</span> RectWidth<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Final perimeter of the rectangle: "</span><span class="token punctuation">,</span> perimeterOfRectangle<span class="token punctuation">)</span></div>

STDIN Input

Please enter the length-
12
Please enter the width-
6

Output

Entered Length: 12
Entered Width: 6
Final perimeter of the rectangle: 36

In the above code, we find the perimeter of the rectangle using the mathematical formula as shown in the below code ?

var perimeterOfRectangle = 2 * (RectangleLength + RectangleWidth)

Here, the length and width of the rectangle are given by the user at run time and using these values the perimeter of the rectangle is calculated that is 36.

Updated on: 2022-08-02T14:50:01+05:30

638 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements