Sling Academy
Home/Kotlin/Understanding the Remainder Operator (`%`) in Kotlin

Understanding the Remainder Operator (`%`) in Kotlin

Last updated: November 29, 2024

The Remainder Operator, represented as %, is a fundamental part of arithmetic operations in many programming languages, including Kotlin. It is used to find the remainder of a division between two numbers. In this article, we will dive into the specifics of how this operator works in Kotlin and provide illustrative examples.

Basic Usage

The remainder operator is used to divide one number by another and returns the remainder of that division. The syntax is straightforward:

val result = dividend % divisor

Here, dividend is the number you want to divide, and divisor is the number by which you want to divide the dividend.

Example:

Let's start with a simple example.


fun main() {
    val dividend = 10
    val divisor = 3
    val remainder = dividend % divisor
    println("The remainder of division is: $remainder")
}

In this example, 10 / 3 leaves a remainder of 1 because 3 can go into 10 three times, which sums to 9, leaving a remainder of 1.

Using with Negative Numbers

The behavior of the remainder operator with negative numbers can sometimes lead to confusion. The remainder sign follows the dividend sign. Let's explore this:


fun main() {
    val positive = 10
    val negative = -10
    val divisor = 3

    println("Positive remainder: ${positive % divisor}")
    println("Negative remainder: ${negative % divisor}")
}

This will output:


Positive remainder: 1
Negative remainder: -1

Notice that when the negative dividend is used, the remainder is also negative.

Real-World Use Cases

The remainder operator is often used in scenarios such as checking if a number is even or odd, cycling through values in a loop, and within algorithms that benefit from modulo arithmetic.

Check if a number is Even or Odd

You can determine if a number is even or odd by checking the remainder when divided by 2:


fun isEven(number: Int): Boolean {
    return number % 2 == 0
}

fun main() {
    println("4 is even: ${isEven(4)}")
    println("5 is even: ${isEven(5)}")
}

This code will output:


4 is even: true
5 is even: false

Conclusion

Understanding the remainder operator is vital for performing accurate and efficient calculations in Kotlin programming. Through examples and detailed explanation, we've explored its usage with both positive and negative integers and demonstrated practical use cases. This knowledge will assist you in numerous coding tasks, from simple arithmetic checks to more complex algorithm implementations.

Next Article: Incrementing and Decrementing Numbers in Kotlin

Previous Article: How Division Works in Kotlin (Integer vs. Float)

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin