# Swift Program to calculate the volume and area of the Cylinder

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

A cylinder is a three-dimensional shape that has two identical parallel circular bases joined by a curved surface.

## Volume of the Cylinder

The amount of space occupied by the cylinder in the three-dimensional plane is known as the volume of the cylinder. For example, we want to fill a cylindrical bottle with shampoo, so using volume we can calculate the required amount of shampoo. We can calculate the volume of the cylinder using radius and height of the cylinder.

### Formula

Following is the formula for the volume of the cylinder −

Volume = πr2h

Below is a demonstration of the same −

Input

Suppose our given input is −

Height = 14

Output

The desired output would be −

Volume of the cylinder = 2814.8670176164546

### Algorithm

Following is the algorithm −

• Step 1 − Declare two double-type variables to store the height and radius of the cylinder −

var cRadius : Double = 5.0
var cHeight : Double = 15.0

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

• Step 2 − Declare a variable named cVolume to store the volume of the cylinder using the following formula −

• Step 3 − Print the output

### Example

The following program shows how to find the volume of the cylinder.

import Foundation import Glibc var cRadius : Double = 5.0 var cHeight : Double = 15.0 // Calculating the volume of the cylinder var cVolume = Double.pi * cRadius * cRadius * cHeight print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the volume of the cylinder is:", cVolume)

### Output

Radius of the cylinder is: 5.0
Height of the cylinder is: 15.0
Hence the volume of the cylinder is: 1178.0972450961724

Here, in the above code we calculate the volume of the cylinder using the following mathematical formula −

Display the result 1178.0972450961724(Volume = 3.141592653589793 * 5 * 5 * 15 = 1178.0972450961724).

## Area of the Cylinder

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

• Curved surface area
• Total surface area

Below is a demonstration of the same −

Input

Suppose our given input is −

Height = 8

Output

The desired output would be

Area = 2814.8670176164546

## 1. Curved Surface Area

The space occupied by the curved surface of the cylinder, or we can say the region occupied between two parallel bases is known as the curved surface area of the cylinder. It is also known as lateral surface area of the cylinder.

### Formula

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

Area = 2πrh

### Algorithm

Following is the algorithm −

• Step 1 − Declare two double-type variables to store the height and radius of the cylinder −

var cRadius : Double = 8.0
var cHeight : Double = 14.0

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

• Step 2 − Declare variable named cArea to store the curved surface area of the cylinder using the following formula −

var cArea = 2 * Double.pi * cRadius * cHeight
• Step 3 − Print the output

### Example

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

import Foundation import Glibc var cRadius : Double = 8.0 var cHeight : Double = 14.0 // Calculating the curved surface area of the cylinder var cArea = 2 * Double.pi * cRadius * cHeight print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the curved surface area of the cylinder is:", cArea)

### Output

Radius of the cylinder is: 8.0
Height of the cylinder is: 14.0
Hence the curved surface area of the cylinder is: 703.7167544041137

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

var cArea = 2 * Double.pi * cRadius * cHeight

Display the result 703.7167544041137 (CSA = 2 * 3.141592653589793 * 8 * 14 = 703.7167544041137).

## 2. Total Surface Area

The sum of the areas of all the faces of the cylinder is known as the total surface area. Or in other words, the sum of the area of two circular bases and the area of the curved surface is known as the total surface area.

### Formula

Following is the formula for the total surface area of a cylinder −

Area = 2πr(h+r)

### Algorithm

Following is the algorithm −

• Step 1 − Declare two double-type variables to store the height and radius of the cylinder −

var cRadius : Double = 4.0
var cHeight : Double = 8.0

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

• Step 2 − Declare a variable named cArea to store the total surface area of the cylinder using the following formula −

var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius)
• Step 3 − Print the output

### Example

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

import Foundation import Glibc var cRadius : Double = 4.0 var cHeight : Double = 8.0 // Calculating the total surface area of the cylinder var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius) print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the total surface area of the cylinder is:", cArea)

### Output

Radius of the cylinder is: 4.0
Height of the cylinder is: 8.0
Hence the total surface area of the cylinder is: 301.59289474462014

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

var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius)

Display the result 301.59289474462014 (TSA = 2 * 3.141592653589793 * 4 * (8 + 4) = 301.59289474462014).

Updated on: 25-Aug-2022

284 Views