Home » Ruby programming » Ruby programs

# Ruby program to find the area of the rectangle

**Finding area of the rectangle**: Here, we are going to learn **how to find the area of the rectangle in Ruby programming?**

Submitted by Hrithik Chandra Prasad, on August 06, 2019

## Finding area of the rectangle

Given "length" and "breadth" and we have to find the area of the rectangle.

The general formula to **find the area of rectangle** is as follows:

area = length * breadth

We can easily find the area of a rectangle by creating logic which applies the above formula. Remember, convert the string into the float instead of integer because we expect the length and breadth to be of float type so as the result.

**Methods used:**

**gets**: It is used to display the message to the user.**puts**: It is used to take input from the user.**.to_f**: It used for converting a value into float type.

**Variables used:**

**l**: It is storing length given by the user.**w**: It contains width provided by the user.**area**: It is containing the product of length and breadth which is eventually the area of rectangle.

## Ruby code to find the area of the rectangle

=begin Ruby program to find Area of Rectangle. =end # input length and breadth, and # convert them to float value puts "Enter length:" l=gets.chomp.to_f puts "Enter width:" w=gets.chomp.to_f # calculating area area=l*w # printing the result puts "Area of Rectangle is #{area}"

**Output**

First run: Enter length: 12.3 Enter width: 5 Area of Rectangle is 61.5 Second run: Enter length: 1.234 Enter width: 0.78 Area of Rectangle is 0.96252

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.