Kotlin Program to Add Two Integers

In this program, you’ll learn to store and add two integer numbers in Kotlin. After addition, the final sum is displayed on the screen.

Example: Kotlin Program to Add Two Integers

  1. fun main(args: Array<String>) {
  2. val first: Int = 10
  3. val second: Int = 20
  4. val sum = first + second
  5. println("The sum is: $sum")
  6. }

When you run the program, the output will be:

The sum is: 30

In this program, similar to Java, two integers 10 and 20 are stored in integer variables first and second respectively. It is not mandatory to add :Int in the variable declaration, Kotlin automatically assigns a type (in this case Int).

Then, first and second are added using the + operator, and its result is stored in another variable sum.

Finally, sum is printed on the screen using println() function.