find the surface area of a cone in swift

To find the surface area of a cone in Swift, you can use the formula:

main.swift
A = πr² + πr(r² + h²)
23 chars
2 lines

where A is the surface area of the cone, r is the radius of the circular base of the cone, and h is the height of the cone.

Here's how you can implement this formula in Swift:

main.swift
func surfaceAreaOfCone(radius r: Double, height h: Double) -> Double {
    let π = Double.pi
    let baseArea = π * r * r
    let slantHeight = sqrt(pow(r, 2) + pow(h, 2))
    let lateralArea = π * r * slantHeight
    let surfaceArea = baseArea + lateralArea
    return surfaceArea
}
284 chars
9 lines

You can then call this function with the radius and height of the cone as arguments:

main.swift
let radius = 3.0
let height = 4.0
let surfaceArea = surfaceAreaOfCone(radius: radius, height: height)
print("The surface area of a cone with radius \(radius) and height \(height) is \(surfaceArea).")
200 chars
5 lines

This will output:

main.swift
The surface area of a cone with radius 3.0 and height 4.0 is 75.39822368615503.
80 chars
2 lines

Note that the sqrt function is used to calculate the slant height l of the cone by applying the Pythagorean theorem (l² = r² + h²).

gistlibby LogSnag