Kotlin Program to Add Two Numbers

In this program, we will learn to code the Kotlin Program to Add Two Numbers. Let’s understand How to add two numbers in Kotlin Programming Language.

We will be using two different ways to perform this task. They are as follows: 

  1. Using Standard Method
  2. Using User Input

Let’s see the code of the Kotlin Program to Add Two Numbers.

Kotlin Program to Add Two Numbers

// Kotlin Program to Add Two Numbers
fun main() {
    var first = 10;
    var second = 20;
    
    var sum = first + second;
    
    println("Sum of Two Numbers is "+sum);
}

Output

Sum of Two Numbers is 30

Kotlin Program to Add Two Numbers Taking User Input

Now, let’s the code of the Kotlin Program to Add Two Numbers by taking the numbers as input from the user.

// Kotlin Program to Add Two Numbers

import java.util.Scanner;

fun main(args: Array<String>) {
    
    var sc = Scanner(System.`in`);
    
    println("Enter First Number : ");
    var first = sc.nextInt();
    
    println("Enter Second Number : ");
    var second = sc.nextInt();
    
    var sum = first + second;
    
    println("Sum of Two Numbers is "+sum);
}

Output

Enter First Number : 10
Enter Second Number : 20
Sum of Two Numbers is 30

How Does This Program Work ?

    var sc = Scanner(System.`in`);
    
    println("Enter First Number : ");
    var first = sc.nextInt();
    
    println("Enter Second Number : ");
    var second = sc.nextInt();

In this program, we take the input of two integer numbers from the user, using the Scanner class in Kotlin. We import the Scanner Class using import java.util.Scanner;

var sum = first + second;

Then, using the addition arithmetic operator “+”, we calculate the sum of the two numbers.

    println("Sum of Two Numbers is "+sum);

Then, we print the sum of the two numbers using the println() method in Kotlin.

Conclusion

I hope after going through this post, you understand Kotlin Program to Add Two Numbers. We discussed two approaches in this post. We also learned to code the Kotlin Program to Add Two Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Learn More:

Leave a Comment

Your email address will not be published.