Kotlin Program to add two numbers using the secondary constructor


In this program, You will learn how to add two numbers using the secondary constructor in Kotlin.


20 = 10 + 10

30 = 10 + 20

Example: How to add two numbers using the secondary constructor in Kotlin.

import java.util.Scanner

class Test {
    constructor(a: Int, b: Int) {
        println("Sum is:${a + b}")
    }
}

fun main(args: Array<String>) {

    var sc = Scanner(System.`in`)

    print("Enter two numbers:")
    var a = sc.nextInt()
    var b = sc.nextInt()

    var obj = Test(a, b)
}

Output:

Enter two numbers:10 20
Sum is:30