0
0
KotlinHow-ToBeginner · 3 min read

How to Use Arithmetic Operators in Kotlin: Simple Guide

In Kotlin, you use +, -, *, /, and % as arithmetic operators to perform addition, subtraction, multiplication, division, and remainder operations. These operators work with numbers like Int and Double to calculate values directly in expressions.
📐

Syntax

Kotlin uses simple symbols for arithmetic operations:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division
  • % for remainder (modulus)

You write expressions like val result = a + b where a and b are numbers.

kotlin
val a = 10
val b = 3
val sum = a + b
val difference = a - b
val product = a * b
val quotient = a / b
val remainder = a % b
💻

Example

This example shows how to use all basic arithmetic operators in Kotlin and prints the results.

kotlin
fun main() {
    val a = 15
    val b = 4

    println("Addition: " + (a + b))
    println("Subtraction: " + (a - b))
    println("Multiplication: " + (a * b))
    println("Division: " + (a / b))
    println("Remainder: " + (a % b))
}
Output
Addition: 19 Subtraction: 11 Multiplication: 60 Division: 3 Remainder: 3
⚠️

Common Pitfalls

One common mistake is dividing two integers and expecting a decimal result. In Kotlin, dividing two Int values returns an Int by dropping the decimal part.

To get a decimal result, convert one or both numbers to Double before dividing.

kotlin
fun main() {
    val x = 7
    val y = 2

    // Wrong: integer division
    println("Integer division: " + (x / y))

    // Right: convert to Double for decimal division
    println("Decimal division: " + (x.toDouble() / y))
}
Output
Integer division: 3 Decimal division: 3.5
📊

Quick Reference

OperatorMeaningExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 3 = 1 (Int division)
%Remainder5 % 3 = 2

Key Takeaways

Use +, -, *, /, and % for basic arithmetic in Kotlin.
Integer division drops decimals; convert to Double for decimal results.
Arithmetic operators work with numeric types like Int and Double.
Remember % gives the remainder after division.
Always check types to avoid unexpected results in calculations.