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

# print the areas
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

CONTRIBUTOR

okwudili onyejiaku
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 