Write a Kotlin Program to Compute Quotient and Remainder

In Kotlin, you can easily compute the quotient and remainder of two integers using the built-in operators div and rem.

Here’s a simple program that takes two integers as input and outputs their quotient and remainder:

fun main() {
    print("Enter the first number: ")
    val num1 = readLine()!!.toInt()

    print("Enter the second number: ")
    val num2 = readLine()!!.toInt()

    val quotient = num1 / num2
    val remainder = num1 % num2

    println("Quotient: $quotient")
    println("Remainder: $remainder")
}

Let’s go over how this Kotlin program works:

The main function is the entry point of the program.

It prompts the user to enter two integers and reads them from the standard input using the readLine function.

The !! operator is used to force a non-null value, since readLine can potentially return null.

The div operator is used to compute the quotient of num1 and num2. This is assigned to the quotient variable.

The rem operator is used to compute the remainder of num1 and num2. This is assigned to the remainder variable.

Finally, the program outputs the quotient and remainder using the println function, along with appropriate labels.

And that’s it! With just a few lines of code, you can easily compute the quotient and remainder of two integers in Kotlin.