Ruby program to calculate the area of the rhombus

Ruby Example: Write a program to calculate the area of the rhombus.
Submitted by Nidhi, on December 06, 2021

Problem Solution:

In this program, we will read diagonal1, diagonal2 from the user and then we will calculate the area of the rhombus and print the result.

Program/Source Code:

The source code to calculate the area of the rhombus is given below. The given program is compiled and executed successfully.

# Ruby program to calculate the 
# area of rhombus

diagonal1=0.0
diagonal2=0.0
area=0.0

print "Enter diagonal1: ";
diagonal1 = gets.chomp.to_f;  

print "Enter diagonal2: ";
diagonal2 = gets.chomp.to_f;  


area = 0.5 * diagonal1 * diagonal2;

print "Area of rhombus is: ",area;

Output:

Enter diagonal1: 2.3
Enter diagonal2: 1.2
Area of rhombus is: 1.38

Explanation:

In the above program, we created three variables diagonal1, diagonal2, area initialized with 0. Then we read values diagonal1, diagonal2 from the user. After that, we calculated the area of the rhombus and printed the result.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.