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
fun main(args: Array<String>) {val first: Int = 10val second: Int = 20val sum = first + secondprintln("The sum is: $sum")}
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.