Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Swift Program to calculate the volume and area of Sphere
This tutorial will discuss how to write a Swift program to calculate the volume and area of the Sphere.
A sphere is a three-dimensional round solid shape or object. Or we can say a sphere is a shape that is defined in three axes: x-axis, y-axis, and z-axis. It does not hold any vertices or edges.

Volume of the Sphere
The amount of space occupied by a sphere in the three-dimensional plane is known as the volume of a sphere. For example, we want to fill a spherical ball with liquid, so using volume we can calculate the required amount of liquid.
Formula
Following is the formula for volume ?
Volume = 4?r<sup>3</sup>/3
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Radius = 3
Output
The desired output would be ?
Volume = 50
Algorithm
Following is the algorithm ?
Step 1 ? Declare a variable of double type to store the radius of the sphere.
var sRadius : Double = 4.0
Step 2 ? Declare another variable to store the volume of the sphere using the mathematical formula ?
var sVolume = (4 * Double.pi * sRadius * sRadius * sRadius)/3
Step 3 ? Print the output.
Example
The following program shows how to find the volume of the sphere.
<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> sRadius <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">4.0</span> <span class="token comment">// Calculating the volume of the sphere </span> <span class="token keyword">var</span> sVolume <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">4</span> <span class="token operator">*</span> Double<span class="token punctuation">.</span>pi <span class="token operator">*</span> sRadius <span class="token operator">*</span> sRadius <span class="token operator">*</span> sRadius<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">3</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Radius of the sphere is:"</span><span class="token punctuation">,</span> sRadius<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 sphere is:"</span><span class="token punctuation">,</span> sVolume<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 sphere is: 4.0 Hence the volume of the sphere is: 268.082573106329
Here in the above code, we calculate the volume of the sphere using the following code ?
var sVolume = (4 * Double.pi * sRadius * sRadius * sRadius)/3
Display the volume of the sphere which is 268.082573106329 (Volume = (4 * 3.141592653589793 * 4.0 * 4.0 * 4.0)/3 = 268.082573106329).
Area of the Sphere
The total space or region covered by the sphere in the three-dimensional plane is known as the area of the sphere.
Formula
Following is the formula for the area of the sphere ?
Area = 4?r<sup>2</sup>
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Radius = 2
Output
The desired output would be ?
Area = 50.26548245743669
Algorithm
Following is the algorithm ?
Step 1 ? Declare a variable of double type to store the radius of the sphere.
var sRadius : Double = 6.0
Step 2 ? Declare another variable to store the area of the sphere using the mathematical formula ?
var sArea = 4 * Double.pi * sRadius * sRadius
Step 3 ? Print the output.
Example
The following program shows how to find the area of a sphere.
<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> sRadius <span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">6.0</span> <span class="token comment">// Calculating the area of the sphere</span> <span class="token keyword">var</span> sArea <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">*</span> Double<span class="token punctuation">.</span>pi <span class="token operator">*</span> sRadius <span class="token operator">*</span> sRadius <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Radius of the sphere is:"</span><span class="token punctuation">,</span> sRadius<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Hence the area of the sphere is:"</span><span class="token punctuation">,</span> sArea<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 sphere is: 6.0 Hence the area of the sphere is: 452.3893421169302
Here in the above code, we calculate the area of the sphere using the following code ?
var sArea = 4 * Double.pi * sRadius * sRadius
Display the area of the sphere which is 452.3893421169302 (Volume = 4 * 3.141592653589793 * 6.0 * 6.0 = 452.3893421169302).
