Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

How to find the area of a rectangle in Ruby

okwudili onyejiaku

Overview

The total space occupied by a rectangle is known as its area. This area, on the other hand, is the product of its length and breadth. The length refers to the longest side, while the breadth refers to the shorter side. In Ruby, we can calculate the area of a rectangle.

Syntax

area = length * breadth
An equation to calculate the area of a rectangle

Parameters

  • length: This is the length of the rectangle.
  • breadth: This is the breadth of the rectangle.

Return value

The value returned is the area of the rectangle.

Example

# create some lengths
length1 = 23
length2 = 4.5
length3 = 50

# create some breadths
breadth1 = 3.5
breadth2 = 0.5
breadth3 = 12

# print the areas 
puts length1 * breadth1
puts length2 * breadth2
puts length3 * breadth3
Get the area of the rectangle in Ruby

Explanation

  • Lines 2–4: We declare and initialize the lengths of the rectangles.
  • Lines 7–9: We declare and initialize the breadths of the rectangles.
  • Lines 12–14: We print the products of the lengths and breadths.

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring