Trusted answers to developer questions

okwudili onyejiaku

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.

area = 6 * a * a

Syntax to get the area of a cube in Ruby

: This represents the area of the cube.**area**

: This represents the area of one face of the cube.**a**

- The value returned is the area of the cube specified.

# create function to get the area of a cube def getArea(faceArea) area = 6 * faceArea * faceArea puts "Area of cube with face area of each side as #{faceArea} is = #{area}" end # create the sides or face area of side of a cube a1 = 12 a2 = 15.111 a3 = 3 a4 = 1.5 # get the area of each cube getArea(a1) getArea(a2) getArea(a3) getArea(a4)

Get the area of a cube in Ruby

- 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.

RELATED TAGS

ruby

communitycreator

CONTRIBUTOR

okwudili onyejiaku

RELATED COURSES

View all Courses

Keep Exploring

Related Courses