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 of a cube
In this tutorial, we will be discussing the approach to finding the volume of a cube in Golang programming using the sides of the cube.
But before writing the code for this, let?s briefly discuss the cube and its volume.
Cube
A cube is a three-dimensional figure that has six square faces. All six faces of the cube are in the shape of a square. Its length, breadth, and height are equal. Dice is a common example of a cube.
Volume of a cube
The total three-dimensional space occupied by the cube is known as the volume of a cube. Calculating the volume of a cube can be beneficial in situations where we want to know the capacity of a cubical object.

$$\mathrm{Length\, = \, Breadth \, =\, Height}$$
Formula
The volume of the cube can be calculated by multiplying the side length three times. Therefore, the formula can be written as
$$\mathrm{Volume \, =\, side * side * side}$$
Example
-
Side = 6
Volume of cube = 6 * 6 * 6 = 216
Since, volume is calculated by multiplying the side length three times, therefore we multiply the side length- ?6? three times, i.e. 6 * 6 * 6, which results in 216 as the volume of the cube.
-
Side = 12.5
Volume of cube = 12.5 * 12.5 * 12.5 = 1953.125
Multiplying the side length- ?12.5? three times, i.e. 12.5 * 12.5 * 12.5, which results in 1953.125 as the volume of the cube.
Finding the volume of a cube within the function
Algorithm
Step 1 ? Declare a variable for storing the side of the cube- ?side?.
Step 2 ? Declare a variable for storing the volume of the cube- ?volume? and initialize it with 0.
Step 3 ? Calculate the volume by multiplying the side length three times and storing it in the ?volume? variable within the main function.
Step 4 ? Print the calculated volume, i.e, the value stored in the variable ?volume?.
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">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <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">"Program to find the volume of a cube \n"</span><span class="token punctuation">)</span>
<span class="token comment">// declaring variable ?side? for storing length of cube</span>
<span class="token keyword">var</span> side float64 <span class="token operator">=</span> <span class="token number">6</span>
<span class="token comment">// declaring variable ?volume? for storing the volume of cube</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 cube</span>
volume <span class="token operator">=</span> side <span class="token operator">*</span> side <span class="token operator">*</span> side
<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">"Dimension of the side : "</span><span class="token punctuation">,</span> side<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 cube : "</span><span class="token punctuation">,</span> volume<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 find the volume of a cube Dimension of the side : 6 Therefore, Volume of cube : 216
Description of code
var side float64 = 5, var volume float64 = 0 ? In this line, we are declaring the variables side and volume. As the side and volume will be of data type float, that?s why we are using the float64 data type.
volume = side * side * side ? We find the cube's volume using the formula- volume = side * side * side.
fmt.Println("Dimension of the side : ", side) ? printing the side of the cube.
fmt.Println("Therefore, Volume of cube : ", volume) ? printing the calculated volume of the cube.
Therefore, volume = 6 * 6 * 6
volume = 216
Finding the volume of a cube using a different function
Algorithm
Step 1 ? Declare a variable for storing the side of the cube- ?side?.
Step 2 ? Declare a variable for storing the volume of the cube- ?volume? and initialize it with 0.
Step 3 ? Calculate the volume by multiplying the side length three times and storing it in the ?volume? variable in the function calculateVolumeOfCube().
Step 4 ? Print the calculated volume, i.e, the value stored in the variable ?volume? by calling the calculateVolumeOfCube() function from within the main() function.
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">calculateVolumeOfCube</span><span class="token punctuation">(</span>side float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>
<span class="token comment">// declaring variable ?volume? for storing the volume of cube</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 cube</span>
volume <span class="token operator">=</span> side <span class="token operator">*</span> side <span class="token operator">*</span> side
<span class="token keyword">return</span> volume
<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 ?side? for storing length of cube</span>
<span class="token keyword">var</span> side float64 <span class="token operator">=</span> <span class="token number">15</span>
<span class="token keyword">var</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 find the volume of a cube \n"</span><span class="token punctuation">)</span>
<span class="token comment">// calling function calculateVolumeOfCube() for calculating</span>
<span class="token comment">// the volume of the cube</span>
volume <span class="token operator">=</span> <span class="token function">calculateVolumeOfCube</span><span class="token punctuation">(</span>side<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">"Dimension of the side : "</span><span class="token punctuation">,</span> side<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 cube : "</span><span class="token punctuation">,</span> volume<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 find the volume of a cube Dimension of the side : 15 Therefore, Volume of cube : 3375
Description of code
var side float64 = 5, var volume float64 ? In this line, we are declaring the variables side and volume. As the side and volume will be of data type float, that?s why we are using the float64 data type.
calculateVolumeOfCube(side float64) float64 ? This is the function where we are calculating the volume of the cube. The function has the variable ?side? of data type float64 as the parameter and also has a return type of float64.
volume = side * side * side ? We are finding the volume of the cube using this formula- side * side * side.
return volume ? for returning the calculated volume of the cube.
volume = calculateVolumeOfCube(side) ? We are calling the function calculateVolumeOfCube() and storing the calculated value in the ?volume? variable.
fmt.Println("Dimension of the side : ", side) ? printing the side of the cube.
fmt.Println("Therefore, Volume of cube : ", volume) ? printing the calculated volume of the cube.
Therefore, volume = 15 * 15 * 15
volume = 3375
Conclusion
This is all about calculating the volume of a cube using two ways. The second way is much better in terms of code reusability and modularity as we can call that function from anywhere, any number of times by passing different values. You can explore more about Golang programming using these tutorials.
