Golang program to calculate the volume and area of a Sphere

In this tutorial, we will be discussing the approach to calculate the volume and area of a sphere using its radius in Golang programming.

But before writing the code for this, let?s briefly discuss the sphere and its volume and area.

Sphere

A sphere is a three-dimensional figure whose all points are equidistant from the center. It has no edges and no faces. The radius of the sphere is denoted by ?r?. A ball is a good example of a sphere.


Volume of a sphere

The capacity or amount of space a sphere occupies is termed its volume. Calculating the volume of a sphere can be beneficial in situations where we want to know how much air we can fill in a football.

$$\mathrm{Volume \, =\, \left ( 4/3 \right )\ast \pi \ast \left ( r \right )^{3} }$$

Area of a sphere

The total area occupied by the sphere is known as the surface area of the sphere. Calculating the area of a sphere can be beneficial in situations where we want to know the cost of painting the surface of a football.

$$\mathrm{Area \, =\, \left ( 4 \right )\ast \pi \ast \left ( r \right )^{2}}$$

Example

  • Radius = 5

    Volume of sphere = 523.8095238095239

    Area of sphere = 314.2857142857143

    Explanation

    Volume of sphere = (4/3) * ? * (r)3

    = (4/3) * (22/7) * (5*5)

    = 523.8095238095239

    Area of sphere = 4 * ? * (r)2

    = 4 * (22/7) * 5 * 5

    = 314.2857142857143

  • Radius = 30

    Volume of sphere = 113142.85714285714

    Area of sphere = 11314.285714285714

Calculating the volume and area of a sphere

Algorithm

Step 1 ? Declare a variable for storing the radius of the sphere- ?r?.

Step 2 ? Declare two variables for storing the area of the sphere- ?area? and volume of the sphere- ?volume? and initialize both the variables with the value 0.

Step 3 ? Calculate the volume using the formula- Volume = (4/3) * ? * (r)3, and store it in the ?volume? variable in the function calculateVolumeOfSphere().

Step 4 ? Calculate the area using the formula- Area = 4 * ? * (r)2, and store it in the ?area? variable in the function calculateAreaOfSphere().

Step 5 ? Print the calculated volume and area of the sphere, 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">calculateVolumeOfSphere</span><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 sphere</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 sphere using its formula</span>
   volume <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">4.0</span> <span class="token operator">/</span> <span class="token number">3.0</span><span class="token punctuation">)</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> r <span class="token operator">*</span> r
   <span class="token keyword">return</span> volume
<span class="token punctuation">}</span>
func <span class="token function">calculateAreaOfSphere</span><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 sphere</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 sphere using its formula</span>
   area <span class="token operator">=</span> <span class="token number">4</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> r
   <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 ?radius? for storing the radius of the sphere</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 variables ?area? and ?volume? for storing the area and volume of the sphere</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 Sphere \n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// calling function calculateVolumeOfSphere() for</span>
   <span class="token comment">// calculating the volume of the sphere</span>
   volume <span class="token operator">=</span> <span class="token function">calculateVolumeOfSphere</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>
   
   <span class="token comment">// calling function calculateAreaOfSphere() for calculating</span>
   <span class="token comment">// the area of the sphere</span>
   area <span class="token operator">=</span> <span class="token function">calculateAreaOfSphere</span><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">"Radius of the sphere : "</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 the sphere : "</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 the sphere : "</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 Sphere 

Radius of the sphere :  5
Therefore, Volume of the sphere :  523.8095238095239
Area of the sphere :  314.2857142857143

Description of code

  • var r float64 = 5 ? In this line, we are declaring the variable for storing the radius ?r?. As the volume and area will be of data type float, that?s the reason we are using the float64 data type.

  • calculateVolumeOfSphere(r float64) float64 ? This is the function where we are calculating the volume of the sphere. The function has the variable ?r? of data type float64 as the parameter and also has a return type of float64.

  • volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r ? 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 4.0 and 3.0 instead of 4 and 3. Using the above formula, we can calculate the volume of the sphere.

  • return volume ? for returning the volume of the sphere.

  • volume = calculateVolumeOfSphere(r) ? We are calling the function calculateVolumeOfSphere() and storing the calculated value in the ?volume? variable in the main method.

  • calculateAreaOfSphere(r float64) float64 ? This is the function where we are calculating the area of the sphere. The function has the variable ?r? of data type float64 as the parameter and also has a return type of float64.

  • area = 4 * (22.0 / 7.0) * r * r ? 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 this formula, we calculate the area of the sphere.

  • return area ? for returning the area of the sphere.

  • area = calculateAreaOfSphere(r) ? We are calling the function calculateAreaOfSphere() and storing the calculated value in the ?area? variable.

  • fmt.Println("Therefore, Volume of the sphere : ", volume) ? and fmt.Println("Area of the sphere : ", area) ? for printing the results

Conclusion

This is all about calculating the volume and area of a sphere 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

Updated on: 2022-11-21T06:33:21+05:30

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements