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





Comments and Discussions

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




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.