- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

- Related Articles
- Java Program to Find the Perimeter of a Rectangle
- Kotlin Program to Find the Perimeter of a Rectangle
- Swift Program to Find the Perimeter of a Circle
- Swift Program to find the area of the rectangle
- JavaScript program to find Area and Perimeter of Rectangle
- How to find the Perimeter of a Rectangle in Golang?
- Program to find Perimeter / Circumference of Square and Rectangle in C++
- C Program for Area And Perimeter Of Rectangle
- Swift Program to Print Solid Rectangle Star Pattern
- Swift program to print hollow rectangle star pattern
- Swift Program to Calculate Area and Perimeter of Equilateral Triangle
- The length of a rectangle is 50 cm if the perimeter of the rectangle is 150 cm. Find the area of the rectangle.
- The length of rectangle is $25\ m$ and width of rectangle is $56\ cm$ find the perimeter of rectangle.
- Find the Perimeter of Triangle ABC.Find the Perimeter of Rectangle BCDE. from the following figure."\n
- A rectangle has a length 15 cm and the perimeter is 50 cm. Find the breadth of the rectangle.