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.

import Foundation import Glibc var RectangleLength = 10 var RectangleWidth = 5 var perimeterOfRectangle = 2 * (RectangleLength + RectangleWidth) print("Length of the rectangle:", RectangleLength) print("Width of the rectangle:", RectangleWidth) print("Final perimeter of the rectangle: ", perimeterOfRectangle)

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.

import Foundation import Glibc print("Please enter the length-") var RectLength = Int(readLine()!)! print("Please enter the width-") var RectWidth = Int(readLine()!)! var perimeterOfRectangle = 2 * (RectLength + RectWidth) print("Entered Length:", RectLength) print("Entered Width:", RectWidth) print("Final perimeter of the rectangle: ", perimeterOfRectangle)

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: 02-Aug-2022

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements