 Trending Categories
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 MS Excel
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP
 Physics
 Chemistry
 Biology
 Mathematics
 English
 Economics
 Psychology
 Social Studies
 Fashion Studies
 Legal Studies
 Selected Reading
 UPSC IAS Exams Notes
 Developer's Best Practices
 Questions and Answers
 Effective Resume Writing
 HR Interview Questions
 Computer Glossary
 Who is Who
Golang program to calculate the volume and area of the Cylinder
In this tutorial, we will be discussing the approach to calculate the volume and area of a cylinder in Golang programming using its height and radius.
But before writing the code for this, let’s briefly discuss the cylinder and its volume and area.
Cylinder
A cylinder is a threedimensional figure whose base has a circular shape. The distance between the two bases is known as the cylinder’s height ‘h’ and the radius of the base is denoted by ‘r’. A cold drink can is a good example of a cylinder.
Volume of a cylinder
The capacity of the cylinder is generally termed its volume. Calculating the volume of a cylinder can be beneficial in situations where we want to know the capacity of a bottle or a container.
$$\mathrm{Volume \, =\, \pi * \left ( r \right )^{2} * h}$$
Area of a cylinder
The total area enclosed by the cylinder is known as the surface area of a cylinder.
$$\mathrm{Area \, =\, 2\ast \pi * r * \left ( h+r \right )}$$
Example

Height = 7, Radius = 5
Volume of cylinder = 550
Area of cylinder = 377.1428571428571
Explanation
Volume of cylinder = π * (r)^{2} * h
= (22/7) * 5 * 5 * 7
= 550
Area of cylinder = 2 * π * r * (h + r)
= 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571

Height = 21, Radius = 10
Volume of cylinder = 6600
Area of cylinder = 1948.5714285714284
Explanation
Volume of cylinder = π * (r)^{2} * h
= (22/7) * 10 * 10 * 21
= 6600
Area of cylinder = 2 * π * r * (h + r)
= 2 * (22/7) * 10 * (21 + 10)
= 1948.5714285714284
Calculating the volume and area of a cylinder:
Algorithm
Step 1 − Declare a variable for storing the height of the cylinder ‘h’.
Step 2 − Declare a variable for storing the radius of the cylinder ‘r’.
Step 3 − Declare a variable for storing the area of the cylinder ‘area’ and declare another variable for storing the volume ‘volume’ and initialize both the variables with value 0.
Step 4 − Calculate the volume using the formula Volume = π * (r)^{2} * h, and store it in the ‘volume’ variable in the function calculateVolumeOfCylinder().
Step 5 − Calculate the area using the formula Area = 2 * π * r * (h + r), and store it in the ‘area’ variable in the function calculateAreaOfCylinder().
Step 6 − Print the calculated volume and area of the cylinder, 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 calculateVolumeOfCylinder(h, r float64) float64 { // declaring variable ‘volume’ for storing the volume of the cylinder var volume float64 = 0 // calculating the volume of the cylinder using formula volume = (22.0 / 7.0) * r * r * h return volume } func calculateAreaOfCylinder(h, r float64) float64 { // declaring variable ‘area’ for storing the area of the cylinder var area float64 = 0 // calculating the area of the cylinder using formula area = 2 * (22.0 / 7.0) * r * (h + r) return area } func main() { // declaring variable ‘height’ for storing the height of the cylinder var h float64 = 7 // declaring variable ‘radius’ for storing the radius of the cylinder var r float64 = 5 // declaring variable ‘area’ and ‘volume’ for storing the area and volume of the cylinder var area, volume float64 fmt.Println("Program to calculate the volume and area of the Cylinder \n") // calling function calculateVolumeOfCylinder() for // calculating the volume of the cylinder volume = calculateVolumeOfCylinder(h, r) // calling function calculateAreaOfCylinder() for calculating // the area of the cylinder area = calculateAreaOfCylinder(h, r) // printing the results fmt.Println("Height of the cylinder : ", h) fmt.Println("Radius of the cylinder : ", r) fmt.Println("Therefore, Volume of cylinder : ", volume) fmt.Println("Area of cylinder : ", area) }
Output
Program to calculate the volume and area of the Cylinder Height of the cylinder : 7 Radius of the cylinder : 5 Therefore, Volume of cylinder : 550 Area of cylinder : 377.1428571428571
Description of code
var h float64 = 7, var r float64 = 5 − In this line, we are declaring the variables for height ‘h’ and radius ‘r’. As the height and radius will be of data type float, that’s why we are using the float64 data type.
calculateVolumeOfCylinder(h, r float64) float64 − This is the function where we are calculating the volume of the cylinder. The function has the variable ‘h’ and ‘r’ of data type float64 as the parameter and also has a return type of float64.
volume = (22.0 / 7.0) * r * r * h − 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 the above formula, we calculate the volume of the cylinder.
return volume − for returning the volume of the cylinder.
volume = calculateVolumeOfCylinder(h, r) − We are calling the function calculateVolumeOfCylinder() and storing the calculated value in the ‘volume’ variable.
calculateAreaOfCylinder(h, r float64) float64 − This is the function where we are calculating the area of the cylinder. The function has the variable ‘h’ and ‘r’ of data type float64 as the parameter and also has a return type of float64.
area = 2 * (22.0 / 7.0) * r * (h + r) − Since we are desiring the output to be of data type float, we explicitly need to convert the values here as well, that is the reason we are using values 22.0 and 7.0 instead of 22 and 7. Using the above formula, we calculate the area of the cylinder.
return area − for returning the area of the cylinder
area = calculateAreaOfCylinder(h, r) − We are calling the function calculateAreaOfCylinder() and storing the calculated value in the ‘area’ variable.
Therefore, Volume of the cylinder = (22/7) * 5 * 5 * 7
= 550
Area of the cylinder = 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571
Conclusion
This is all about calculating the volume and area of a cylinder 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.
 Related Articles
 Swift Program to calculate the volume and area of the Cylinder
 Haskell Program to calculate the volume and area of the Cylinder
 Python Program to calculate the volume and area of the Cylinder
 C++ Program to calculate the volume and area of the Cylinder
 Golang program to calculate the volume and area of a Cone
 Golang program to calculate the volume and area of a Sphere
 Golang program to Calculate the Volume, Diagonal and Area of a Cuboid?
 Golang program to calculate the volume of a cube
 Swift Program to calculate the volume and area of Sphere
 Python Program to calculate the volume and area of Cone
 Python Program to calculate the volume and area of Sphere
 Haskell Program to calculate the volume and area of Cone
 Haskell Program to calculate the volume and area of Sphere
 C++ Program to calculate the volume and area of Sphere
 Haskell Program to calculate the volume, diagonal, and area of Cuboids