Kotlin Program to Add Two Numbers

The most common program when we start learning any language after Hello World Program is to add two numbers. From as easy it may sound to making it modular and complex even to the extent adding 2 numbers without using the “+” operator.

Below are the examples to add 2 numbers in different ways:

Using Arithmetic Operator (+)

The easiest and the simplest way is to use the plus operator to add 2 numbers

fun main(args: Array<String>) {
	val num1 = 55
	val num2 = 25
	val sum = num1+num2;
	println("The sum is "+sum)
}

The output of the above code is as follows.

Output:

The sum is 80

Using Difference Operator (-)

The next way is to pass the negation of a number and then find the difference of two.

fun main(args: Array<String>) {
	val num1 = 55
	val num2 = 25
	val sum = num1 - (-num2);
	println("The sum is "+sum)
}

The output of the above code is as follows:

Output:

The sum is 80

Comment down below if you have any queries related to above program.

Leave a Comment

Your email address will not be published. Required fields are marked *