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

package main // fmt package allows us to print formatted strings import "fmt" func main() { fmt.Println("Program to find the volume of a cube \n") // declaring variable ‘side’ for storing length of cube var side float64 = 6 // declaring variable ‘volume’ for storing the volume of cube var volume float64 = 0 // calculating the volume of the cube volume = side * side * side // printing the results fmt.Println("Dimension of the side : ", side) fmt.Println("Therefore, Volume of cube : ", volume) }

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

package main // fmt package allows us to print formatted strings import "fmt" func calculateVolumeOfCube(side float64) float64 { // declaring variable ‘volume’ for storing the volume of cube var volume float64 = 0 // calculating the volume of the cube volume = side * side * side return volume } func main() { // declaring variable ‘side’ for storing length of cube var side float64 = 15 var volume float64 fmt.Println("Program to find the volume of a cube \n") // calling function calculateVolumeOfCube() for calculating // the volume of the cube volume = calculateVolumeOfCube(side) // printing the results fmt.Println("Dimension of the side : ", side) fmt.Println("Therefore, Volume of cube : ", volume) }

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.

Updated on: 21-Nov-2022

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements