Golang program to calculate the area of a cube


In this tutorial, we will be discussing the approach to finding the surface area 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 area.

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.

Area of a cube

The total area enclosed by all six faces of the cube is known as the surface area of a cube. Calculating the area of a cube can be beneficial in situations where we want to paint the cube's surface or wrap a sheet around it.


Formula

The sum of the areas of all the sides of the cube is defined as the surface area of the cube. Therefore, the formula can be written as six times the square of the side.

$$\mathrm{Area\, =\, 6\ast \left ( side \right )^{2}}$$

Example

  • Side = 5

    Area of cube = 6 * (5)2 = 150

    Since, the area is calculated by multiplying the square of the side by 6, therefore we multiply 6 and (5)2, i.e. 6 * 25, which results in 150 as the area of the cube.

  • Side = 10.5

    Area of cube = 6 * (10.50)2 = 661.50

    Multiplying 6 and (10.50)2, i.e. 6 * 110.25, which results in 661.50 as the area of the cube.

Finding the area of a cube within the main function

Algorithm

Step 1 − Declare a variable for storing the side of the cube- ‘side’.

Step 2 − Declare a variable for storing the area of the cube- ‘area’ and initialize it with 0.

Step 3 − Calculate the area by multiplying the square of the side by 6 and store it in the ‘area’ variable within the function.

Step 4 − Print the calculated area, i.e, the value stored in the variable ‘area’.

Example

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

Output

Program to find the area of a cube

Dimension of the side : 5 
Therefore, Area of cube : 150

Description of code

  • var side float64 = 5, var area float64 = 0 − In this line, we are declaring the variables side and area. As the side and area will be of data type float, that’s the reason why we are using the float64 data type.

  • area = 6 * math.Pow(side, 2) − We are finding the area of the cube using the formula- area = 6 * (side)2. math.Pow is a maths function used to calculate a number's power.

  • fmt.Println("Dimension of the side : ", side) − printing the side of the cube.

  • fmt.Println("Therefore, Area of cube : ", area) − printing the calculated area of the cube.

Therefore, area = 6 * (5)2

area = 6 * 25

area = 150

Finding the area 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 area of the cube- ‘area’ and initialize it with 0 initially.

Step 3 − Calculate the area by multiplying the square of the side by 6 and store it in the ‘area’ variable in the function calculateAreaOfCube().

Step 4 − Print the calculated area, i.e, the value stored in the variable ‘area’ by calling the calculateAreaOfCube() function from within the main() function.

Example

package main // fmt package allows us to print formatted strings // math package allows us to perform various mathematical // operations import ( "fmt" "math" ) func calculateAreaOfCube(side float64) float64 { // declaring variable ‘area’ for storing the area of cube var area float64 = 0 // calculating the area of the cube area = 6 * math.Pow(side, 2) return area } func main() { // declaring variable ‘side’ for storing length of cube var side float64 = 15 var area float64 fmt.Println("Program to find the area of a cube \n") // calling function calculateAreaOfCube() for calculating // the area of the cube area = calculateAreaOfCube(side) // printing the results fmt.Println("Dimension of the side : ", side) fmt.Println("Therefore, Area of cube : ", area) }

Output

Program to find the area of a cube 

Dimension of the side :  15
Therefore, Area of cube :  1350

Description of code

  • var side float64 = 5, var area float64 = 0 − In this line, we are declaring the variables side and area. As the side and area will be of data type float, we are using the float64 data type.

  • calculateAreaOfCube(side float64) float64 − This is the function where we are calculating the area of the cube. The function has the variable ‘side’ of data type float64 as the parameter and also has a return type of float64.

  • area = 6 * math.Pow(side, 2) − We are finding the area of the cube using this formula- area = 6 * (side)2. math.pow is a maths function that is used to calculate the power of a number.

  • return area − for returning the area of the cube

  • area = calculateAreaOfCube(side) − We are calling the function calculateAreaOfCube() and storing the calculated value in the ‘area’ variable.

  • fmt.Println("Dimension of the side : ", side) − printing the side of the cube

  • fmt.Println("Therefore, Area of cube : ", area) − printing the calculated area of the cube

Conclusion

This is all about calculating the area of a cube using two ways. The second way is much better in terms of modularity and code reusability. You can explore more about Golang programming using these tutorials.

Updated on: 21-Nov-2022

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements