0
0
Kotlinprogramming~5 mins

Arithmetic operators in Kotlin

Choose your learning style9 modes available
Introduction

Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, and divide values easily.

Calculating the total price of items in a shopping cart.
Finding the average score of a student.
Increasing or decreasing a counter in a game.
Converting units, like kilometers to miles.
Splitting a bill evenly among friends.
Syntax
Kotlin
val sum = a + b
val difference = a - b
val product = a * b
val quotient = a / b
val remainder = a % b
Use + for addition, - for subtraction, * for multiplication, / for division, and % for remainder (modulus).
Make sure the variables used are numbers like Int or Double.
Examples
Adds 10 and 3, then prints 13.
Kotlin
val a = 10
val b = 3
val sum = a + b
println(sum)
Finds the remainder when 10 is divided by 3, which is 1.
Kotlin
val a = 10
val b = 3
val remainder = a % b
println(remainder)
Multiplies two decimal numbers and prints 11.0.
Kotlin
val a = 5.5
val b = 2.0
val product = a * b
println(product)
Sample Program

This program shows all basic arithmetic operations with two numbers and prints the results.

Kotlin
fun main() {
    val x = 15
    val y = 4

    val sum = x + y
    val difference = x - y
    val product = x * y
    val quotient = x / y
    val remainder = x % y

    println("Sum: $sum")
    println("Difference: $difference")
    println("Product: $product")
    println("Quotient: $quotient")
    println("Remainder: $remainder")
}
OutputSuccess
Important Notes

Division between two integers returns an integer result (it cuts off decimals).

Use Double or Float types if you want decimal results from division.

The remainder operator (%) gives the leftover part after division.

Summary

Arithmetic operators let you do basic math in Kotlin.

Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.

Integer division drops decimals; use floating-point numbers for precise division.