find the volume of a cylinder in ruby

math, cylinder, volume, formula, ruby

def cylinder_volume(radius, height) Math::PI * radius**2 * height end

puts cylinder_volume(3, 5) #=> 141.3716694115407

main.rb

Explanation:
- `Math` module is used to get the value of Pi.
- `cylinder_volume` function accepts two arguments - `radius` and `height`.
- The function calculates the volume of the cylinder using the formula `πr²h`.
- The calculated volume is returned by the function.
- The `puts` function is used to output the calculated volume for the given input values of radius=3 and height=5.
385 chars
8 lines

related categories

gistlibby LogSnag