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 = ?r<sup>2</sup>h

Below is a demonstration of the same ?

Input

Suppose our given input is ?

Radius = 8 
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 ?

var cVolume = Double.pi * cRadius * cRadius * cHeight
  • Step 3 ? Print the output

Example

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

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation 
<span class="token keyword">import</span> Glibc

<span class="token keyword">var</span> cRadius <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">5.0</span> 
<span class="token keyword">var</span> cHeight <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">15.0</span>

<span class="token comment">// Calculating the volume of the cylinder </span>
<span class="token keyword">var</span> cVolume <span class="token operator">=</span> Double<span class="token punctuation">.</span>pi <span class="token operator">*</span> cRadius <span class="token operator">*</span> cRadius <span class="token operator">*</span> cHeight

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Radius of the cylinder is:"</span><span class="token punctuation">,</span> cRadius<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height of the cylinder is:"</span><span class="token punctuation">,</span> cHeight<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the volume of the cylinder is:"</span><span class="token punctuation">,</span> cVolume<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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 ?

var cVolume = Double.pi * cRadius * cRadius * cHeight

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 ?

Radius = 4 
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.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation 
<span class="token keyword">import</span> Glibc

<span class="token keyword">var</span> cRadius <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">8.0</span> 
<span class="token keyword">var</span> cHeight <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">14.0</span>

<span class="token comment">// Calculating the curved surface area of the cylinder </span>
<span class="token keyword">var</span> cArea <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> Double<span class="token punctuation">.</span>pi <span class="token operator">*</span> cRadius <span class="token operator">*</span> cHeight

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Radius of the cylinder is:"</span><span class="token punctuation">,</span> cRadius<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height of the cylinder is:"</span><span class="token punctuation">,</span> cHeight<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the curved surface area of the cylinder is:"</span><span class="token punctuation">,</span> cArea<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation 
<span class="token keyword">import</span> Glibc

<span class="token keyword">var</span> cRadius <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">4.0</span> 
<span class="token keyword">var</span> cHeight <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">8.0</span>

<span class="token comment">// Calculating the total surface area of the cylinder </span>
<span class="token keyword">var</span> cArea <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> Double<span class="token punctuation">.</span>pi <span class="token operator">*</span> cRadius <span class="token operator">*</span> <span class="token punctuation">(</span>cHeight <span class="token operator">+</span> cRadius<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Radius of the cylinder is:"</span><span class="token punctuation">,</span> cRadius<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Height of the cylinder is:"</span><span class="token punctuation">,</span> cHeight<span class="token punctuation">)</span> 
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the total surface area of the cylinder is:"</span><span class="token punctuation">,</span> cArea<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-08-25T08:56:31+05:30

534 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements