find the surface area of a cone in python

To find the surface area of a cone in Python, we need to use the formula:

S = πr(l + r)

where S is the surface area, r is the radius of the base of the cone, and l is the slant height of the cone.

Here is the Python code to calculate the surface area of a cone:

main.py
import math

# Function to calculate the surface area of a cone
def cone_surface_area(radius, height):
    # calculate the slant height
    l = math.sqrt(radius**2 + height**2)
    # calculate the surface area
    surface_area = math.pi * radius * (radius + l)
    # return the result
    return surface_area
309 chars
11 lines

To use the function, simply pass in the radius and the height of the cone as arguments:

main.py
# Example usage
radius = 5
height = 10
surface_area = cone_surface_area(radius, height)
print("Surface area of cone with radius", radius, "and height", height, "is", surface_area)
180 chars
6 lines

Output:

main.py
Surface area of cone with radius 5 and height 10 is 219.9114857512855
70 chars
2 lines

gistlibby LogSnag