1. Overview

Performing basic arithmetic operations is a fundamental skill in almost all programming languages.

In this tutorial, we’ll explore the essentials of computing the quotient and remainder in Kotlin.

2. Introduction to the Problem

Computing quotient and remainder isn’t a challenge for us. We’ll first address how to compute these values.

As the quotient and the remainder are two values, we’ll discuss how to get these values in one shot by creating a Kotlin function. Kotlin offers many excellent features that allow us to write idiomatic code. So, we’ll try to improve our function using some Kotlin features.

For simplicity, we’ll use integers as examples in this tutorial.

3. Using the ‘/’ and ‘%’ Operators

In Kotlin, computing the quotient and remainder is straightforward, thanks to the division operator ‘/’ and the modulus operator ‘%’:

val quotient1 = 42 / 4
val reminder1 = 42 % 4
assertEquals(10, quotient1)
assertEquals(2, reminder1)

val quotient2 = -42 / 4
val reminder2 = -42 % 4
assertEquals(-10, quotient2)
assertEquals(-2, reminder2)
val quotient3 = -42 / 2
val reminder3 = -42 % 2
assertEquals(-21, quotient3)
assertEquals(0, reminder3)

As the example above shows, we can compute the quotient and remainder in this way:

  • Quotient = Dividend / Divisor
  • Remainder = Dividend % Divisor

4. Creating a Function to Return the Quotient and the Remainder

Now that we understand how to compute the quotient and remainder using the ‘/’ and the ‘%’ operators, let’s create a function so that we can get the two values in a single function call:

fun getQuotientAndReminder(dividend: Int, divisor: Int): Pair<Int, Int> {
    return dividend / divisor to dividend % divisor
}

As we can see, to have a function return multiple values, we make the function return a Pair object.

Next, let’s verify whether the function works as expected:

getQuotientAndReminder(42, 4).apply {
    assertEquals(10, first)
    assertEquals(2, second)
}

getQuotientAndReminder(-42, 4).apply {
    assertEquals(-10, first)
    assertEquals(-2, second)
}
getQuotientAndReminder(-42, 2).apply {
    assertEquals(-21, first)
    assertEquals(0, second)
}

The test passes when we give it a run. However, our function doesn’t handle the “zero as the divisor” case. For instance, let’s pass 0 as the divisor to the function:

getQuotientAndReminder(42, 0)

We’ll get an ArithmeticException:

java.lang.ArithmeticException: / by zero

Let’s say we’d like to handle this case and throw an IllegalArgumentException with a customized message when the divisor is zero. All we need to do is add a require() call to the function:

fun getQuotientAndReminder(dividend: Int, divisor: Int): Pair<Int, Int> {
    require(divisor != 0) { "The divisor must not be zero" }
    return dividend / divisor to dividend % divisor
}

Now, we’ll get the expected exception when we pass zero as the divisor to the function:

assertThrows<IllegalArgumentException> {
    getQuotientAndReminder(42, 0)
}.also { assertEquals("The divisor must not be zero", it.message) }

5. Creating an Extension Function

The getQuotientAndReminder() function does the job. However, as we code and call this function, we must first type the function name and then pass the dividend and the divisor arguments to it.

A more natural and fluent way to call a function to compute the quotient and the remainder would be, for example, 42 dividedBy 4.

Next, let’s see if how this can be achieved:

infix fun Int.dividedBy(divisor: Int): Pair<Int, Int> {
    require(divisor != 0) { "The divisor must not be zero" }
    return this / divisor to this % divisor
}

First, we make dividedBy() as an extension function of Int, so all Int can call it as a member function, for example, 42.dividedBy(2). Then, we add infix keyword to call the function without typing the period and brackets.

Finally, let’s compare the code of two function calls:

val (quotient, reminder) = 42 dividedBy 4
val (quotient, reminder) = getQuotientAndReminder(42, 4)

As we can see, the dividedBy() function call is easier to read and natural to type. It’s worth noting that we’ve used Kotlin’s deconstructing declaration to assign the two variables in one line.

6. Conclusion

In this article, first, we explored computing the quotient and remainder using the division and the modulus operators. Then, we created a function to return a Pair object so that we can obtain these two values in one single function call.

Finally, we polished our function by making it an infix extension function.

As always, the complete source code for the examples is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.