Home »
Ruby »
Ruby Programs
Ruby program to calculate the volume, diagonal, and area of Cuboids
Ruby Example: Write a program to calculate the volume, diagonal, and area of Cuboids.
Submitted by Nidhi, on November 29, 2021
Problem Solution:
In this program, we will read the length, height, width of Cuboids from the user. Then we will calculate the diagonal, volume, and surface area of Cuboids and print the result.
Program/Source Code:
The source code to calculate the volume, diagonal, and area of Cuboids is given below. The given program is compiled and executed successfully.
# Ruby program to calculate the
# volume, diagonal, and area of Cuboids
length=0.0;
height=0.0;
width =0.0;
diagonal=0.0;
volume=0.0;
area =0.0;
print "Enter length: ";
length = gets.chomp.to_f;
print "Enter height: ";
height = gets.chomp.to_f;
print "Enter width: ";
width = gets.chomp.to_f;
diagonal = Math.sqrt((width * width)) + (length * length) + (height * height);
area = 2.0 * (width * length) + (length * height) + (height * width);
volume = width * length * height;
print "Diagonal of Cuboids is: ",diagonal;
print "\nVolume of Cuboids is: ",volume;
print "\nArea of Cuboids is: ",area;
Output:
Enter length: 12.3
Enter height: 12.5
Enter width: 15.4
Diagonal of Cuboids is: 322.94000000000005
Volume of Cuboids is: 2367.75
Area of Cuboids is: 725.09
Explanation:
In the above program, we created six variables length, height, width, diagonal, volume, area that are initialized with 0.0. Then we read length, height, width of Cuboids from the user and calculated the surface area, volume, and diagonal of Cuboids using the standard formula. After that, we printed the result.
Ruby Basic Programs »