Swift Program to Find the Perimeter of a Circle


This tutorial will discuss how to write a Swift program to find the perimeter of the circle.

Perimeter of the circle is also known as the circumference of the circle. It is used to calculate the boundary of the circle. Suppose we want to fence our circular garden so with the help of perimeter we can calculate the total amount of fence required to cover the boundary of the garden. We can calculate the perimeter of the circle with the help of the radius or diameter, here,

Radius − It is known as the distance from the center to a point on the edge of the circle and generally represented by r or R. Or we can say that radius is half of diameter that is r = D/2.

Diameter − It is known as a line that passes through the centre of the circle and its endpoint is present on the edge of the circle and generally represented by the word d or D. Or we can say that diameter is double of radius that is D = 2r.

Formula

Following is the perimeter or circumference of the circle −

Perimeter = 2 * Pi * r

Following is the perimeter or circumference of the circle in terms of diameter −

Perimeter = 2 * Pi * D

Algorithm to Perimeter using Radius of the circle

  • Step 1 − Define Radius

  • Step 2 − Enter the value of Pi

  • Step 3 − Enter the formula − 2πr

  • Step 4 − Print the output

Example

Perimeter using radius of the circle

The following program shows how to calculate the perimeter of the circle with the help of the radius.

import Foundation
import Glibc

var radius = 10.0
let pi = 3.14

var perimeterOfCircle = 2 * pi * radius

print("Radius of the circle is:", radius)
print("So the perimeter is:", perimeterOfCircle)

Output

Radius of the circle is: 10.0
So the perimeter is: 62.800000000000004

In the above code, we calculate the perimeter of the circle with the help of radius of the circle using the following code −

var perimeterOfCircle = 2 * pi * radius

Here the radius of the circle is 10.0. So the perimeter of the circle is 62.800000000000004.

Algorithm to Perimeter using Diameter of the circle

  • Step 1 − Define Diameter

  • Step 2 − Enter the value of Pi

  • Step 3 − Enter the formula − πd

  • Step 4 − Print the output

Example

Perimeter using diameter of the circle

The following program shows how to calculate the perimeter of the circle with the help of the diameter.

import Foundation
import Glibc

var diameter = 5.0
let pi = 3.14

var perimeterOfCircle = pi * diameter

print("Diameter of the circle is:", diameter)
print("So the perimeter is:", perimeterOfCircle)

Output

Diameter of the circle is: 5.0
So the perimeter is: 15.700000000000001

In the above code, we calculate the perimeter of the circle with the help of the diameter using the following code −

var perimeterOfCircle = pi * diameter

Here the diameter of the circle is 5.0. So the perimeter of the circle is 15.7.

Updated on: 28-Jul-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements