Kotlin Program to Compute Quotient and Remainder

In this program, you’ll learn to compute quotient and remainder from the given dividend and divisor in Kotlin.

Example: Compute Quotient and Remainder

1. fun main(args: Array<String>) {
2. val dividend = 25
3. val divisor = 4
4. val quotient = dividend / divisor
5. val remainder = dividend % divisor
6. println("Quotient = \$quotient")
7. println("Remainder = \$remainder")
8. }

When you run the program, the output will be:

Quotient = 6
Remainder = 1

In the above program, two numbers 25 (dividend) and 4 (divisor) are stored in two variables dividend and divisor respectively. Unlike Java, these are automatically assigned Int type in Kotlin.

Now, to find the quotient we divide dividend by divisor using / operator. Since, both dividend and divisor are Int, the result will also be computed as an Int.

So, mathematically even if 25/4 results 6.25, since both operands are Int, quotient variable only stores 6 (integer part).

Likewise, to find the remainder we use the % operator. So, the remainder of 25/4, i.e. 1 is stored in an Int variable remainder.

Finally, quotient and remainder are printed on the screen using println() function.