- 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
How to find the Surface area and Volume of Cuboid in Golang?
In this tutorial, we will see the Golang program to find the Surface Area and Volume of a Cuboid. The area is the total space covered by any closed figure. The volume is the capacity to hold something inside the vessel.
Formula
l - length of a Cuboid
h - the height of a Cuboid
w - width of a Cuboid.
Surface Area of Cuboid - 2*l*w + 2*w*h + 2*l*h
For example, the length of a Cuboid is 10 cm, the height is 5 cm and, the width is 8 cm so the Surface Area of a Cuboid is −
l = 10 cm
h = 5 cm
w = 4 cm
Area = 2*l*w + 2*w*h + 2*l*h
= 2*10*4 + 2*4*5 + 2*5*10
= 80 + 40 + 100
= 220 cm^2
Volume of Cuboid - l * b * h
For example, the length of a Cuboid is 10 cm, the height is 5 cm and, the width is 8 cm so the volume of a Cuboid is -
l = 10 cm
h = 5 cm
w = 4 cm
Volume = l * b * h
= 10 * 5 * 4
= 200
Algorithm
Step 1 - Declaring the variables for the length , width, height, volume, and the area of the float64 data type.
Step 2 - Initialize the variable.
Step 3 - Find the surface Area and volume using the above formulas within the function.
Step 4 - Printing the result.
Example
In this example, we are going to find the surface Area And volume of a Cuboid within the function.
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the floating variables using the var keyword for // storing the length of the parallel sides of the Cuboid, // distance between the parallel sides and also a variable area // to store Area var l, w, h, surfaceArea, volume float64 fmt.Println("Program to find the Surface Area and volume of a Cuboid.") // initializing the length of a Cuboid l = 10 // initializing the width of a Cuboid w = 8 // initializing the height of a Cuboid h = 4 // finding the surface Area of a Cuboid surfaceArea = 2*l*w + 2*w*h + 2*l*h // finding the volume of a Cuboid volume = l * w * h // printing the result fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.") fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.") fmt.Println("(Finding the Surface Area and volume of a Cuboid within the function)") }
Output
Program to find the Surface Area and volume of a Cuboid. The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm. The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm. (Finding the Surface Area and volume of a Cuboid within the function)
Algorithm
Step 1 - Declaring the variables for the length , width, height, volume, and the area of the float64 data type.
Step 2 - Initialize the variable.
Step 3 - Calling the function with the length, width and height of the Cuboid and the as a parameter, and storing the Area the function is returning.
Step 4 - Calling the function with the length, width and height of the Cuboid and the as a parameter, and storing the volume the function is returning.
Step 5 - Printing the result.
Example
In this example, we are going to find the Area of a Cuboid by defining the separate function to find the Area.
package main // fmt package provides the function to print anything import ( "fmt" ) // in this line we have declared the function that has float64 // type parameter and float64 type returntype func areaOfCuboid(l, w, h float64) float64 { // returning the area by applying the formula return 2*l*w + 2*w*h + 2*l*h } // in this line we have declared the function that has float64 // type parameter and float64 type returntype func volumeOfCuboid(l, w, h float64) float64 { // returning the volume by applying the formula return l * w * h } func main() { // declaring the floating variables using the var keyword for // storing the length of the parallel sides of the Cuboid, // distance between the parallel sides and also a variable area // to store Area var l, w, h, surfaceArea, volume float64 fmt.Println("Program to find the Surface Area and volume of a Cuboid.") // initializing the length of a Cuboid l = 10 // initializing the width of a Cuboid w = 8 // initializing the height of a Cuboid h = 4 // finding the surface Area of a Cuboid by calling the function surfaceArea = areaOfCuboid(l, w, h) // finding the volume of a Cuboid volume = volumeOfCuboid(l, w, h) // printing the result fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.") fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.") fmt.Println("(Finding the Surface Area and volume of a Cuboid in the seperate function)") }
Output
Program to find the Surface Area and volume of a Cuboid. The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm. The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm. (Finding the Surface Area and volume of a Cuboid in the seperate function)
Conclusion
These are the two ways to find the Area and volume of a Cuboid in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.