In this topic, you will explore the approach to division in Kotlin. You will learn how to apply division to positive and negative numbers. In addition, we will talk about several built-in Kotlin functions that you can use to perform division.
Division with positive numbers
Before we start writing code, let's figure out what the Euclidean division formula is. With it, any number can be represented by three others. If we denote the initial number as the dividend, the formula looks like this:
dividend = divisor * quotient + remainder
Now, let's understand how to get the quotient and remainder values with the dividend = 16 and the divisor = 5.
val quotient = 16 / 5
val remainder = 16 % 5
println("The quotient is: " + quotient) // The quotient is: 3
println("The remainder is: " + remainder) // The remainder is: 1
Using this knowledge, let's check if the calculation is correct:
// The dividend = 5 * 3 + 1
print("The dividend = " + (divisor * quotient + remainder)) // The dividend = 16
Note that in both examples we get a whole number when dividing with the / operator, since we're working with the Int type.
Division with negative numbers
In the previous section, we operated with positive numbers; now let's apply the same algorithm to a negative number (-13) with 4 as the divisor. As you already know, there are several division types. Kotlin uses T-division by default. Let's recap what is it:
val quotient: Int = -13 / 4
val remainder: Int = -13 % 4
println("The quotient is: " + quotient) // The quotient is: -3
println("The remainder is: " + remainder) // The remainder is: -1
print("The dividend = " + (4 * (-3) + (-1)) // The dividend = -13
Here, the dividend is a negative number. The division result is: -13 / 4 = -3.25 . If you truncate the fraction part of the result, you receive -3. So, the quotient is -3.
Let's calculate the remainder now! As you know, remainder = dividend - divisor * quotient . In our case:
-13 - 4 * (-3) = -13 + 12 = -1
As you can see, everything is correct. Let's look at how it works with different signs:
Expression | Quotient (/ result) | Remainder (% result) | The result |
-13 / 4 | -3 | -1 | -13 = 4 * (-3) + (-1) |
13 / -4 | -3 | 1 | 13 = (-4) * (-3) + 1 |
-13 / -4 | 3 | -1 | -13 = (-4) * 3 + (-1) |
You don't have to memorize the signs! Just knowing how it works is enough.
Below are some division examples in Kotlin:
println(11 / -4) // prints -2
println(11 % -4) // prints 3
println(-11 / 4) // prints -2
println(-11 % 4) // prints -3
println(-11 / -4) // prints 2
println(-11 % -4) // prints -3Another way to divide numbers
As you know, languages like Python use another type of division, F-division. In Kotlin, you can work with this division approach using the floorDiv() and mod() functions. Let's see how it works:
println((-12).floorDiv(5)) // -3
println((-12).mod(5)) // 3
When you divide -12 by 5, the result is -2.4, which is rounded to -3, since it is the nearest whole number smaller than -2.4.
You can calculate the remainder by the familiar formula: remainder = dividend - divisor * quotient. In our case:
-12 - 5 * (-3) = -12 + 15 = 3
If the mod() function operated similarly in all cases, we would face issues when performing Euclidean division. When both numbers have the same sign, this function performs similarly to the % operator:
Expression | Quotient ( | Remainder ( | The result |
-12 / 5 | -3 | 3 | -12 = 5 * (-3) + 3 |
12 / -5 | -3 | -3 | 12 = (-5) * (-3) + (-3) |
-12 / -5 | 2 | -2 | -12 = (-5) * 2 + (-2) |
Below are some division examples in Kotlin:
println(11.floorDiv(4)) // prints 2
println(11.mod(4)) // prints 3
println(11.floorDiv(-4)) // prints -3
println(11.mod(-4)) // prints -1
println((-11).floorDiv(4)) // prints -3
println((-11).mod(4)) // prints 1
println((-11).floorDiv(-4)) // prints 2
println((-11).mod(-4)) // prints -3
As you can see, with positive numbers, the floorDiv() and mod() functions operate the same way as / and % respectively.
Conclusion
In this topic, you've explored the division formula. You've found out how to apply it to positive and negative numbers. Now it's time to check your knowledge. Move on to the practice section and check out the tasks. Good luck!