Swift Program to calculate the volume and area of Cone

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write a Swift program to calculate the volume and area of Cone.

A cone is a solid geometrical three-dimensional shape. It has a circular base and pointed edge at the top known as apex with one face and a vertex. It does not contain any edge. A cone has three elements: radius, height and slant height.


Volume of a Cone

The amount of space occupied by a cone in the three dimensional plane is known as the volume of a cone. For example, we want to fill a cone with popcorn, so usin g volume we can calculate the required amount of popcorn.

Formula

Following is the formula for the volume of cone −

Volume = πr2 h/3

Below is a demonstration of the same −

Input

Suppose our given input is −

Radius = 3
Height = 9

Output

The desired output would be −

Volume = 50

Algorithm

Following is the algorithm −

Step 1 − Declare two variables of double type to store t he radius and height of the cone −

var coneRadius = 4.0
var coneHeight = 8.0

Step 2 − Declare another variable named coneVolume to store the volume of the cone using the mathematical formula −

var coneVolume = (Double.pi * coneRadius * coneRadius * coneHeight) / 3

Step 3 − Print the output.

Example

The following program shows how to calculate the volume of the cone.

import Foundation import Glibc var coneRadius = 4.0 var coneHeight = 8.0 // Finding the volume of the cone var coneVolume = (Double.pi * coneRadius * coneRadius * coneHeight) / 3 print("Height of the cone is:", coneHeight) print("Radius of the cone is:", coneRadius) print("Cone's volume:", coneVolume)

Output

Height of the cone is: 8.0
Radius of the cone is: 4.0
Cone’s volume: 134.041286553 1645

Here in the above code, we calculate the volume of the cone using the following code: −

var coneVolume = (Double.pi * coneRadius * coneRadius * coneHeight) / 3

Display the volume of the cone which is 134.0412865531645 (Volume = (3.141592653589793 * 4.0 * 4.0 * 8.0)/3 = 134.0412865531645

Area of the Cone

The total space or region covered by the cone in the three dimensional plane is known as the area of the cone. A cone has two types of area −

  • Curved surface area

  • Total surface area

Below is a demonstration of the same −

Input

Suppose our given input is −

Radius = 3
Slant Height = 9

Output

The desired output would be −

Area = 113.09733552923255
  • Curved Surface Area

The space occupied by the curved surface of the cone, or we can say the region enclosed in the curved part of the cone is known as the curved surface area of the cone. We cancalculate the curved surface area of the cone using radius(r) and slant height(l) of the cone.

Formula

Following is the formula for the curved surface area of cone −

Area = πrl

Algorithm

Following is the algorithm

Step 1 − Declare two double type variables to store the slant height and radius of the cone −

var coneRadius = 8.0
var coneSlant = 24.0

Here the value of these variables can be user defined or pre defined.

Step 2 − Declare variable named coneArea to store the curved surface area of the cone using the following formula −

var coneArea = Double.pi * coneRadius * coneSlant

Step 3 − Print the output

Example

The following program shows how to find the curved surface area of cone.

import Foundation import Glibc var coneRadius = 8.0 var coneSlant = 24.0 // Finding the curved surface area of the cone var coneArea = Double.pi * coneRadius * coneSlant print("Slant Height of the cone is:", coneSlant) print("Radius of t he cone is:", coneRadius) print("So, Curved surface area of the cone:", coneArea)

Output

Slant Height of the cone is: 24.0
Radius of the cone is: 8.0
So, Curved surface area of the cone: 603.1857894892403

Here, in the above code we calculate the curved surface area of the cone using the following mathematical formula −

var coneArea = Double.pi * coneRadius * coneSlant

Display the result 603.1857894892403

(CSA = 3.141592653589793 * 8.0 * 24.0 = 603.1857894892403)
  • Total Surface Area

The sum of the area of the circular base and the area of curved part of the cone is known as the total surface area of the cone. We can calculate the total surface area of the cone using radius(r) and slant height(l) of the cone.

Formula

Following is the formula for the total surface area of the cone −

Area = πr2 + πrl = πr(r + l)

Algorithm

Following is the algorithm −

Step 1 − Declare two double type variables to store the slant height and radius of the cone −

var coneRadius = 2.0
var coneSlant = 10.0

Here the value of the se variables can be user defined or pre defined.

Step 2 − Declare a variable named coneArea to store the total surface area of the cone using the following formula −

var coneArea = Double.pi * coneRadius * (coneRadius + coneSlant)

Step 3 − Print the output

Example

The following program shows how to find the total surface area of a cone.

import Foundation import Glibc var coneRadius = 2.0 var coneSlant = 10.0 // Finding the total surface area of the cone var coneArea = Double.pi * coneRadius * (coneRadi us + coneSlant) print("Slant Height of the cone is:", coneSlant) print("Radius of the cone is:", coneRadius) print("So, total surface area of the cone is:", coneArea)

Output

Slant Height of the cone is: 10.0
Radius of the cone is: 2.0
So, total surface area of the cone is: 75.39822368615503

Here, in the above code, we calculate the total surface area of the cone using the following mathematical formula −

var coneArea = Double.pi * coneRadius * (coneRadius + coneSlant)

Display the result 75.39822368615503 (TSA = 3.141592653589793 * 2.0 * (2.0 + 10.0) =75.39822368615503

raja
Updated on 10-Oct-2022 14:58:36

Advertisements