Write a Kotlin Program to Add Two Integers

In Kotlin, adding two integers is a straightforward process. We can use the plus operator (+) to add two integers.

Here’s an example Kotlin program that adds two integers and prints the result to the console:

fun main() {
    val num1 = 5
    val num2 = 10
    val sum = num1 + num2
    println("The sum of $num1 and $num2 is $sum")
}

In this program, we first declare two variables num1 and num2 and assign them the values of 5 and 10, respectively.

We then add these two variables using the plus operator and store the result in the sum variable.

Finally, we use the println function to print the result to the console.

The output of this program would be:

The sum of 5 and 10 is 15

It’s important to note that when adding two integers, the result may overflow if the sum exceeds the maximum value that can be represented by the integer data type.

In such cases, it’s recommended to use a larger data type like Long to store the result.

In conclusion, adding two integers in Kotlin is a simple process that involves using the plus operator.

It’s important to be aware of potential overflow issues when working with large numbers.