find the area of a triangle in swift

Here's a sample code to find the area of a triangle in Swift:

main.swift
func areaOfTriangle(base: Double, height: Double) -> Double {
    let area = 0.5 * base * height
    return area
}

// Example usage:
let base = 10.0
let height = 5.0
let area = areaOfTriangle(base: base, height: height)
print("The area of the triangle with base \(base) and height \(height) is \(area)")
305 chars
11 lines

In the code above, we define a function called areaOfTriangle that takes two parameters, base and height, both of type Double. The function then calculates the area of the triangle using the formula 0.5 * base * height and returns the result.

To use the function, we pass in the values for the base and height of the triangle and store the result in a variable called area. We then print out the result using a formatted string that displays the values of the base, height, and area.

gistlibby LogSnag