find the area of a rectangle in swift

To find the area of a rectangle in Swift, you need to multiply its length by its width. Here's an example code snippet that takes user input for length and width, and outputs the area of the rectangle:

main.swift
import Foundation

print("Enter length of rectangle:")
let length = Double(readLine()!)!

print("Enter width of rectangle:")
let width = Double(readLine()!)!

let area = length * width
print("The area of the rectangle is \(area).")
232 chars
11 lines

In this code, we first import the Foundation library so that we can use readLine() to take user input. We then prompt the user to enter the length and width of the rectangle, and store these values in constants. We calculate the area of the rectangle by multiplying the length and width, and print the result.

Note that we use the ! operator to unwrap the optional values returned by readLine(). In production code, you should add error handling around user input to handle cases where the user enters invalid input.

gistlibby LogSnag