find the surface area of a cylinder in ruby

To find the surface area of a cylinder in Ruby, we need to use the formula:

Surface Area = 2πr(h + r)

where r is the radius of the base of the cylinder, and h is the height of the cylinder.

Here's the Ruby code to find the surface area of a cylinder:

main.rb
def surface_area_of_cylinder(radius, height)
  surface_area = 2 * Math::PI * radius * (height + radius)
  return surface_area
end
130 chars
5 lines

In this code, we're defining a method called surface_area_of_cylinder, which takes two arguments, the radius and the height of the cylinder. We're using the Math::PI constant to represent the value of pi in our calculation.

To test our method, we can call it with some sample values:

main.rb
puts surface_area_of_cylinder(3, 5)
# Output: 150.79644737231007
65 chars
3 lines

This means that the surface area of a cylinder with a radius of 3 and a height of 5 is approximately 150.8 square units.

gistlibby LogSnag