How to get the area of a cube in Ruby
Overview
The area of a cube is the sum of all areas of the faces of a cube. A cube has six faces. In Ruby, we calculate the area of a cube by using the formula or logic in the syntax below.
Syntax
area = 6 * a * a
Syntax to get the area of a cube in Ruby
Parameters
area: This represents the area of the cube.a: This represents the area of one face of the cube.
Return value
The value returned is the area of the cube specified.
Example
# create function to get the area of a cubedef getArea(faceArea)area = 6 * faceArea * faceAreaputs "Area of cube with face area of each side as #{faceArea} is = #{area}"end# create the sides or face area of side of a cubea1 = 12a2 = 15.111a3 = 3a4 = 1.5# get the area of each cubegetArea(a1)getArea(a2)getArea(a3)getArea(a4)
Explanation
Line 2: We create a function called
getArea()which takes a single parameter. The parameter represents the area of each face or side of the cube.Lines 8–11: We create the sides or face area for the cubes.
Lines 14–17: We invoke the function we created and print the results to the console.