w3resource

Ruby Basic Exercises: Compute the volume

Ruby Basic: Exercise-14 with Solution

Write a Ruby program which accept the radius of the sphere as input and compute the volume.

Ruby Basic Exercises: Compute the volume

Ruby Code:

r = 5.0
v = 0.0
pi = 3.141592653
print "Input the radius of the circle: "
r = gets.to_f
v = (4/3)* (pi)* (r**3)
puts "The volume of the sphere is : #{v}."

Output:

Input the radius of the circle: The volume of the sphere is : 392.699081625.

Flowchart:

Flowchart: Compute the volume

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to create a new string from a given string using the first three characters or whatever is there if the string is less than length 3. Return n copies of the string.
Next: Write a Ruby program to create a new string from a given string where the first and last characters have been exchanged.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.