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

<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 comment">// math package allows us to perform various mathematical</span>
<span class="token comment">// operations</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<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 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 area of a cube \n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// declaring variable ?side? for storing the length of the cube</span>
   <span class="token keyword">var</span> side float64 <span class="token operator">=</span> <span class="token number">5</span>
   
   <span class="token comment">// declaring variable ?area? for storing the area of the cube</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 cube</span>
   area <span class="token operator">=</span> <span class="token number">6</span> <span class="token operator">*</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span>side<span class="token punctuation">,</span> <span class="token number">2</span><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, Area of cube : "</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 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

<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 comment">// math package allows us to perform various mathematical</span>
<span class="token comment">// operations</span>

<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math"</span>
<span class="token punctuation">)</span>
func <span class="token function">calculateAreaOfCube</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 ?area? for storing the area of cube</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 cube</span>
   area <span class="token operator">=</span> <span class="token number">6</span> <span class="token operator">*</span> <span class="token class-name"><span class="token namespace">math<span class="token punctuation">.</span></span>Pow</span><span class="token punctuation">(</span>side<span class="token punctuation">,</span> <span class="token number">2</span><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 ?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> area 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 area of a cube \n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// calling function calculateAreaOfCube() for calculating</span>
   <span class="token comment">// the area of the cube</span>
   area <span class="token operator">=</span> <span class="token function">calculateAreaOfCube</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, Area of cube : "</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 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: 2022-11-21T06:08:19+05:30

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements