find the surface area of a cone in ruby

To find the surface area of a cone in ruby, we first need to know the radius and height of the cone. We can use the formula:

surface area = πr(r + √(h^2 + r^2))

Where π is a mathematical constant, r is the radius of the cone, and h is the height of the cone.

We can write a ruby function to calculate surface area of cone as follows:

main.rb
def surface_area_of_cone(radius, height)
  pi = Math::PI
  surface_area = pi * radius * (radius + Math.sqrt((height ** 2) + (radius ** 2)))
  return surface_area
end
166 chars
6 lines

This function takes two arguments: the radius and the height of the cone, and returns the surface area of the cone.

We can call this function and print the result as follows:

main.rb
radius = 5
height = 10

surface_area = surface_area_of_cone(radius, height)

puts "The surface area of a cone with radius #{radius} and height #{height} is #{surface_area}"
173 chars
7 lines

This will output: The surface area of a cone with radius 5 and height 10 is 219.91148575128568

related categories

gistlibby LogSnag