Kotlin Program to Add Two Integers

May 31, 2020 No comments Kotlin-Examples Kotlin Program

1. Introduction

In this article, we are going to present a Kotlin program to add two integer numbers. The program will display the sum result on the screen at the end.

2. Example: Add Two Integer Numbers in Kotlin


fun main() {
    val a: Int = 21
    val b: Int = 9

    val sum = a + b

    println("The result is: $sum") // 30
}

The output will be:

The result is: 30

In this example, we declared two variables a and b with assigned values 21 and 9. Note that the explicit determination of the variable's type is not mandatory in Kotlin. The :Int statement could be skipped, then Kotlin automatically evaluates a type.

Values stored in a and b variable are added using the + operator. The result of this operation is sored in the sum variable.

In the next step, we used println(...) function to print the sum on the screen.

{{ message }}

{{ 'Comments are closed.' | trans }}