Kotlin Arithmetic Operators Tutorial

In this section, we will learn what the Arithmetic Operators are and how to use them in Kotlin.

Note: we’re assuming you’re already familiar with the Kotlin Operators in General.

What is Arithmetic Operator in Kotlin?

The arithmetic operators are those that we use to run mathematical operations with.

For example, addition +, multiplication *, subtraction - etc. are examples of arithmetic operators.

Arithmetic Operators List in Kotlin

In the table below you can see the list of arithmetic operators:

Operator Symbol
Addition Operator +
Subtraction Operator
Multiplication Operator *
Division Operator /
Modulus Operator %
Increment Operator ++
Decrement Operator

Kotlin Addition + Operator:

The addition operator is used to add two values together and it returns the result.

Addition + Operator Syntax:

Left-Operand + Right-Operand

Example: using addition + operator in Kotlin

val result = 40 + 100

println( result) // 140

Note that using the addition operator, we can concatenate two string values as well!

For example:

val res = “String One “ + “String Two”

println(res) // String One String Two”

Kotlin Subtraction - Operator:

The subtraction operator is used to subtract two values from each other and return the result.

Subtraction - Operator Syntax:

Left-Operand - Right-Operand

Example: using subtraction - operator in Kotlin

var res = 100 – 40

println (res) // 60

Kotlin Multiplication * Operator:

The multiplication operator is used to multiply two values for each other and return its result.

Multiplication * Operator Syntax:

Left-Operand * Right-Operand

Example: using multiplication * operator in Kotlin

fun main(){
    val num1 = 10 
    val num2 = 20 

    val res = num1 * num2

    println(res)
}

Output:

200

Kotlin Division / Operator:

The division operator is used to divide two numbers by each other and return the result.

Division / Operator Syntax:

Left-Operand / Right-Operand

Example: using division operator in Kotlin

fun main(){
    val num1 = 40 
    val num2 = 20 

    val res = num1 / num2

    println(res)
}

Output:

2

Kotlin Modulus % Operator:

The modulus operator is used to find the remainder after dividing one value by another.

Modulus % Operator Syntax:

Left-Operand % Right-Operand

Example: using modulus operator in Kotlin

fun main(){
    val num1 = 50 
    val num2 = 20 

    val res = num1 % num2

    println(res)
}

Output:

10

Kotlin Increment ++ Operator:

The increment operator is used to increase the current value of a variable by 1. This means if for example, the target variable has the value 10, then applying this operator on that variable will result the value 11 to be stored in that variable.

Increment ++ Operator Syntax:

++variableName

variableName++

If we put the ++ operator on the left side of a variable, the value of the variable will increase by one immediately and before any other operation can be done in the program.

But if the ++ operator appears on the right side of a variable, that means the value of that variable must be increased by one the next time we call the variable to get its value! So the increase of the value won’t happen immediately.

Example: using increment operator in Kotlin

fun main(){
    var num = 10
    println(num++)
    println(num)

    var secondNum = 20
    println(++secondNum)
    println(secondNum)
}

Output:

10

11

21

21

Here in the statement:

println(num++)

The value of the num variable will be increased by one the next time we call this variable! But at the moment, the value of the variable stays the same! That’s why the first call to the println function returned the current value of the num variable.

Now the second time we called the println function with the num variable, we can see that the value is changed to 11.

Also in the statement:

println(++secondNum)

Here the increment operator appeared on the left side of the secondNum variable and that means the value of the variable must first increase by one and so the value 20 which is the current value of this variable changes to the value 21 and then this new value will be passed to the println function.

Kotlin Decrement -- Operator:

The decrement operator is used to decrease the current value of a variable by one and store the result in that variable again.

This means if we run this operator on a variable with the value 10 for example, the value will decrease to 9 and the result will be stored in the same variable.

Decrement -- Operator Syntax:

--variableName

variableName--

The effect of this operator when we put it on the left or right side of a variable is exactly the same as the ++ operator.

Example: using decrement operator in Kotlin

fun main(){
    var num = 10
    println(num--)
    println(num)

    var secondNum = 20
    println(--secondNum)
    println(secondNum)
}

Output:

10

9

19

19
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies