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

package main // fmt package allows us to print formatted strings import "fmt" func calculateVolumeOfSphere(r float64) float64 { // declaring variable ‘volume’ for storing the volume of the sphere var volume float64 = 0 // calculating the volume of the sphere using its formula volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r return volume } func calculateAreaOfSphere(r float64) float64 { // declaring variable ‘area’ for storing the area of the sphere var area float64 = 0 // calculating the area of the sphere using its formula area = 4 * (22.0 / 7.0) * r * r return area } func main() { // declaring variable ‘radius’ for storing the radius of the sphere var r float64 = 5 // declaring variables ‘area’ and ‘volume’ for storing the area and volume of the sphere var area, volume float64 fmt.Println("Program to calculate the volume and area of the Sphere \n") // calling function calculateVolumeOfSphere() for // calculating the volume of the sphere volume = calculateVolumeOfSphere(r) // calling function calculateAreaOfSphere() for calculating // the area of the sphere area = calculateAreaOfSphere(r) // printing the results fmt.Println("Radius of the sphere : ", r) fmt.Println("Therefore, Volume of the sphere : ", volume) fmt.Println("Area of the sphere : ", area) }

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: 21-Nov-2022

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements