Q:

Ruby program to calculate the volume and area of Sphere

belongs to collection: Ruby Basic Programs

0

In this program, we will read the radius of Sphere from the user. Then we will calculate the volume and surface area of the Sphere and print the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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

# Ruby program to calculate the 
# volume and area of Sphere

radius=0.0;

volume=0.0;
area  =0.0;

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

volume = (4.0 / 3.0) * (3.14) * radius* radius* radius;
area = 4.0 * (3.14) * radius* radius;

print "Volume of Sphere is: ",volume;
print "\nArea of Sphere is: ",area;

Output:

Enter radius: 4.56
Volume of Sphere is: 396.9747763199999
Area of Sphere is: 261.16761599999995

Explanation:

In the above program, we created three variables radiusvolume, and area, that are initialized with 0.0. Then we read the radius of Sphere from the user and calculated the surface area and volume of Sphere using the standard formula. After that, we printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to read coordinate points and determi... >>
<< Ruby program to calculate the volume and area of C...