Start learning free

LESSON 17 OF 348 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in Kotlin

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

    KOTLIN · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in Kotlin, explained

    Problem overview

    Two variables are already declared: width, which is 23, and height, which is 17. The task is to print the area of the rectangle they describe. The area is the width multiplied by the height, so the program has to compute 23 * 17 and display it, using the variables rather than the typed-in numbers.

    Approach

    This lesson needs one arithmetic operator, *, and one output function, println. A rectangle's area is width times height, and the multiplication can sit directly inside the call. There is no reason to store the area in a variable first, because the value is used once and then forgotten. Both variables are Int values, and the product of two Int values is an Int, so the output has no decimal point.

    Step by step

    1. Start from the two declarations. They already give width and height their values, so the new code goes after them, on the line that follows.
    2. Multiply the variables. Write width * height. Kotlin replaces each name with its value, so this is 23 times 17.
    3. Pass the product to println. The function prints whatever the expression evaluates to and ends the line.

    The solution in Kotlin

    val width: Int = 23
    val height: Int = 17
    
    println(width * height)
    

    The program prints 391. A few details are worth knowing:

    • Use the variables, not the numbers. Writing println(391) would print the right answer for this input only. With width * height, changing a declaration changes the output automatically.
    • val means the variable can't be reassigned. That is the right choice here, since the dimensions never change.
    • The type is inferred if you leave it out. val width = 23 is also an Int. The lesson's : Int just spells it out.
    • Integer results stay integers. If the dimensions were Double values such as 2.5 and 4.0, the same expression would print 10.0.

    Going a little further

    Once the area works, two small variations show how the same idea scales.

    Storing the result

    If you want to use the area again, put it in a variable and print the variable. A string template, written with $ inside double quotes, lets you add a label:

    val width = 23
    val height = 17
    val area = width * height
    
    println("Area: $area")
    println("Area: ${width * height}")
    

    Both lines print Area: 391. A plain name after $ inserts its value, and an expression needs braces: ${...}. The lesson itself needs only the bare number, so keep the output exactly 391 there.

    Other numeric types

    The type of the product follows the types of the operands. An Int times a Double is a Double: with val w = 2.5 and val h = 4, the expression w * h prints 10.0. An Int is also limited to about 2.1 billion, so very large dimensions overflow without any warning:

    val bigW = 100000
    val bigH = 100000
    println(bigW * bigH)              // 1410065408
    println(bigW.toLong() * bigH)     // 10000000000
    

    Converting one operand to Long before multiplying fixes it. The numbers in this lesson are small, so plain Int is fine.

    Edge cases and common mistakes

    • Adding instead of multiplying. width + height gives 40, which is half the perimeter, not the area.
    • Printing the variable names. println("width * height"), with quotes, prints the text width * height. Without quotes, Kotlin evaluates the expression.
    • Writing the code before the declarations. Kotlin reads top to bottom, so a variable must be declared before the line that uses it.
    • Using x for multiplication. The operator is the asterisk, *.

    Testing

    Try other values in the declarations and compare the output with a hand calculation:

    • width = 5, height = 4 prints 20.
    • width = 1, height = 100 prints 100.
    • width = 0, height = 9 prints 0.

    Key takeaways

    • Put the calculation inside println; no helper variable is required.
    • Use the variables so the program still works when their values change.
    • The type of a product follows its operands: Int times Int is an Int, and a Double anywhere makes the result a Double.
    • Declarations come before the lines that use them, because Kotlin reads from top to bottom.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free