Swift Program to Find the Area of a Circle


This tutorial will discuss how to write a swift program to find the area of a Circle.

In a circle, an area is known as the space that is enclosed inside the boundaries of the circle in the two-dimensional plane. Suppose we have a round tea table now the area of the circle helps us to find how much cloth we required to cover the top of the table. We can calculate the area of the circle with the help of the radius or diameter, here,

We are going to use the formula of area of the circle −

Area of the circle = π*r*r

Or

Area of the circle = (π*D*D)/4

Example

Area using Circle Radius

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

import Foundation
import Glibc

var r = 20.0
var pi = 3.14

var areaOftheCircle = pi * r * r

print("Radius of the circle is", r)
print("So the circle's area is", areaOftheCircle)

Output

Radius of the circle is 20.0
So the circle's area is 1256.0

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

var areaOftheCircle = pi*r*r

Here the radius of the circle is 20. So the area of the circle is 1256.0.

Example

Area using Circle Diameter

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

import Foundation
import Glibc

var diameter = 10.0
var pi = 3.14

var areaOfCircle = (pi * diameter * diameter)/4

print("Diameter of the circle is", diameter)
print("So the circle's area is", areaOfCircle)

Output

Diameter of the circle is 10.0
So the circle's area is 78.5

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

var areaOfCircle = (pi * diameter * diameter)/4

Here the diameter of the circle is 10. So the area of the square is 78.5.

Example

Area using Math Library

To calculate the area of the circle we use the math library of Swift. Here, we are using the predefine pow() function to find the power of the radius of the circle. It will take only two arguments one is the base of the power that is radius and another one is an exponent. In the below example, we also use a predefined pi value that is Double.pi.

import Foundation
import Glibc

func areaOfTheCircle(r: Double)->Double{
   let areaOfTheCircle = Double.pi * pow(r, 2)
   return areaOfTheCircle
}

var result = areaOfTheCircle(r: 4)
print("Circle's Area =", result)

Output

Circle's Area = 50.26548245743669

In the above code, we create a function named as areaOfTheCircle() to calculate the area of the circle as shown in the following code −

func areaOfTheCircle(r: Double)->Double{
   let areaOfTheCircle = Double.pi * pow(r, 2)
   return areaOfTheCircle
}

Here the areaOfTheCircle() function takes one argument named as r of double type and return the area of double type. Now we find the area of the circle using the mathematical formula Double.pi * pow(r, 2), where Double.pi is the pre defined value of the pi that is 3.141592653589793 and pow() function returns the power of the radius(r). After calculating the area this function return the area of the circle.

Now we call the function and pass the radius of the circle in it and display the area of the circle that is 50.26548245743669.

Updated on: 29-Jul-2022

706 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements