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.swift232 chars11 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