Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

How to get the area of a cube in Ruby

okwudili onyejiaku

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

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.

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring