Start learning free

LESSON 17 OF 415 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in Swift: Multiplying Two Constants

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in Swift, explained

    Problem overview

    The starter code declares two constants, width and height, holding 23 and 17. The task is to print the area of the rectangle they describe. The area of a rectangle is its width times its height, so the program must print the product of the two values.

    The exercise is a first use of arithmetic on named values: instead of typing numbers into the expression, you use the constants that already exist.

    Approach

    Swift's multiplication operator is *. Because width and height are both whole numbers, Swift infers the type Int for each, and width * height is an Int as well. You don't need an extra variable for the area: print accepts any expression, evaluates it, and prints the result.

    Step by step

    1. Leave the two constants alone. They are declared with let, which is right: a rectangle's sides don't change while the program runs.
    2. Write the expression width * height on the next line. The operands are the names of the constants, not their values typed again.
    3. Wrap it in print. The call writes the value of the expression to the console, followed by a line break.

    The solution in Swift

    let width = 23
    let height = 17
    
    print(width * height)
    

    The program prints:

    391
    

    Swift details worth knowing here:

    • Top-level code. A Swift file can contain statements outside any function, and they run from the first line down. No main is needed.
    • No semicolons. A line break ends a statement.
    • Both sides must have the same type. width * height works because both are Int. Multiplying an Int by a Double doesn't compile in Swift; one side needs an explicit conversion such as Double(width). Rectangles with decimal sides need Double constants for that reason.
    • Whitespace around operators. Write width * height with spaces on both sides, or on neither. Swift treats an operator with space on only one side differently.

    Decimal sides

    If the sides can have decimals, declare them as Double and the same expression works. With let w2 = 4.5 and let h2 = 2.0, print(w2 * h2) prints 9.0. A Double always prints with a decimal point, even when the value is whole.

    Mixing the two kinds is an error. With width as the Int 23 and h2 as a Double, width * h2 fails with binary operator '*' cannot be applied to operands of type 'Int' and 'Double'. Converting one side fixes it: Double(width) * h2 prints 46.0.

    Storing the result

    You can also keep the area in a constant, which helps when you use it more than once. With let area = width * height, the call print("The area is", area) prints The area is 391. For this exercise the extra constant isn't needed, and the task wants only the number.

    Edge cases and common mistakes

    • Adding instead of multiplying. width + height prints 40, which is the sum of the sides, not the area.
    • Typing the numbers. print(23 * 17) prints the same result for this starter code, but it ignores the constants. If the values change, the answer breaks.
    • Printing a label as well. print("Area: ", width * height) prints extra text. The task asks for the number alone.
    • Forgetting print. A bare width * height at the top level is computed and thrown away, and Swift warns that the result is unused. Nothing appears in the console.

    Key takeaways

    • * multiplies, and the area of a rectangle is width * height.
    • print can take an expression directly, so no intermediate variable is needed.
    • Swift does not mix Int and Double in one operation without a conversion.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free