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
Golang program to calculate the volume and area of the Cylinder
In this tutorial, we will be discussing the approach to calculate the volume and area of a cylinder in Golang programming using its height and radius.
But before writing the code for this, let?s briefly discuss the cylinder and its volume and area.
Cylinder
A cylinder is a three-dimensional figure whose base has a circular shape. The distance between the two bases is known as the cylinder?s height ?h? and the radius of the base is denoted by ?r?. A cold drink can is a good example of a cylinder.

Volume of a cylinder
The capacity of the cylinder is generally termed its volume. Calculating the volume of a cylinder can be beneficial in situations where we want to know the capacity of a bottle or a container.
$$\mathrm{Volume \, =\, \pi * \left ( r \right )^{2} * h}$$
Area of a cylinder
The total area enclosed by the cylinder is known as the surface area of a cylinder.
$$\mathrm{Area \, =\, 2\ast \pi * r * \left ( h+r \right )}$$
Example
-
Height = 7, Radius = 5
Volume of cylinder = 550
Area of cylinder = 377.1428571428571
Explanation
Volume of cylinder = ? * (r)2 * h
= (22/7) * 5 * 5 * 7
= 550
Area of cylinder = 2 * ? * r * (h + r)
= 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571
-
Height = 21, Radius = 10
Volume of cylinder = 6600
Area of cylinder = 1948.5714285714284
Explanation
Volume of cylinder = ? * (r)2 * h
= (22/7) * 10 * 10 * 21
= 6600
Area of cylinder = 2 * ? * r * (h + r)
= 2 * (22/7) * 10 * (21 + 10)
= 1948.5714285714284
Calculating the volume and area of a cylinder:
Algorithm
Step 1 ? Declare a variable for storing the height of the cylinder- ?h?.
Step 2 ? Declare a variable for storing the radius of the cylinder- ?r?.
Step 3 ? Declare a variable for storing the area of the cylinder- ?area? and declare another variable for storing the volume- ?volume? and initialize both the variables with value 0.
Step 4 ? Calculate the volume using the formula- Volume = ? * (r)2 * h, and store it in the ?volume? variable in the function calculateVolumeOfCylinder().
Step 5 ? Calculate the area using the formula- Area = 2 * ? * r * (h + r), and store it in the ?area? variable in the function calculateAreaOfCylinder().
Step 6 ? Print the calculated volume and area of the cylinder, i.e, the value stored in the ?volume? and ?area? variables.
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package allows us to print formatted strings</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">calculateVolumeOfCylinder</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> r float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>
<span class="token comment">// declaring variable ?volume? for storing the volume of the cylinder</span>
<span class="token keyword">var</span> volume float64 <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// calculating the volume of the cylinder using formula</span>
volume <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">22.0</span> <span class="token operator">/</span> <span class="token number">7.0</span><span class="token punctuation">)</span> <span class="token operator">*</span> r <span class="token operator">*</span> r <span class="token operator">*</span> h
<span class="token keyword">return</span> volume
<span class="token punctuation">}</span>
func <span class="token function">calculateAreaOfCylinder</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> r float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>
<span class="token comment">// declaring variable ?area? for storing the area of the cylinder</span>
<span class="token keyword">var</span> area float64 <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// calculating the area of the cylinder using formula</span>
area <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">22.0</span> <span class="token operator">/</span> <span class="token number">7.0</span><span class="token punctuation">)</span> <span class="token operator">*</span> r <span class="token operator">*</span> <span class="token punctuation">(</span>h <span class="token operator">+</span> r<span class="token punctuation">)</span>
<span class="token keyword">return</span> area
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring variable ?height? for storing the height of the cylinder</span>
<span class="token keyword">var</span> h float64 <span class="token operator">=</span> <span class="token number">7</span>
<span class="token comment">// declaring variable ?radius? for storing the radius of the cylinder</span>
<span class="token keyword">var</span> r float64 <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment">// declaring variable ?area? and ?volume? for storing the area and volume of the cylinder</span>
<span class="token keyword">var</span> area<span class="token punctuation">,</span> volume float64
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to calculate the volume and area of the Cylinder \n"</span><span class="token punctuation">)</span>
<span class="token comment">// calling function calculateVolumeOfCylinder() for</span>
<span class="token comment">// calculating the volume of the cylinder</span>
volume <span class="token operator">=</span> <span class="token function">calculateVolumeOfCylinder</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> r<span class="token punctuation">)</span>
<span class="token comment">// calling function calculateAreaOfCylinder() for calculating</span>
<span class="token comment">// the area of the cylinder</span>
area <span class="token operator">=</span> <span class="token function">calculateAreaOfCylinder</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span> r<span class="token punctuation">)</span>
<span class="token comment">// printing the results</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Height of the cylinder : "</span><span class="token punctuation">,</span> h<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Radius of the cylinder : "</span><span class="token punctuation">,</span> r<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Therefore, Volume of cylinder : "</span><span class="token punctuation">,</span> volume<span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Area of cylinder : "</span><span class="token punctuation">,</span> area<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Program to calculate the volume and area of the Cylinder Height of the cylinder : 7 Radius of the cylinder : 5 Therefore, Volume of cylinder : 550 Area of cylinder : 377.1428571428571
Description of code
var h float64 = 7, var r float64 = 5 ? In this line, we are declaring the variables for height ?h? and radius ?r?. As the height and radius will be of data type float, that?s why we are using the float64 data type.
calculateVolumeOfCylinder(h, r float64) float64 ? This is the function where we are calculating the volume of the cylinder. The function has the variable ?h? and ?r? of data type float64 as the parameter and also has a return type of float64.
volume = (22.0 / 7.0) * r * r * h ? If we desire the output to be of data type float, we explicitly need to convert the values, that is the reason we are using values 22.0 and 7.0 instead of 22 and 7. Using the above formula, we calculate the volume of the cylinder.
return volume ? for returning the volume of the cylinder.
volume = calculateVolumeOfCylinder(h, r) ? We are calling the function calculateVolumeOfCylinder() and storing the calculated value in the ?volume? variable.
calculateAreaOfCylinder(h, r float64) float64 ? This is the function where we are calculating the area of the cylinder. The function has the variable ?h? and ?r? of data type float64 as the parameter and also has a return type of float64.
area = 2 * (22.0 / 7.0) * r * (h + r) ? Since we are desiring the output to be of data type float, we explicitly need to convert the values here as well, that is the reason we are using values 22.0 and 7.0 instead of 22 and 7. Using the above formula, we calculate the area of the cylinder.
return area ? for returning the area of the cylinder
area = calculateAreaOfCylinder(h, r) ? We are calling the function calculateAreaOfCylinder() and storing the calculated value in the ?area? variable.
Therefore, Volume of the cylinder = (22/7) * 5 * 5 * 7
= 550
Area of the cylinder = 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571
Conclusion
This is all about calculating the volume and area of a cylinder using Go programming. We have also maintained code modularity by using separate functions for calculating the area and volume which also increases the code reusability. You can explore more about Golang programming using these tutorials.
