Arithmetic Operators (Mathematical Operators) in Kotlin

Arithmetic Operator Types

Kotlin has 5 Arithmetic Operators similar to Java. They are

  • Addition operator (+)
  • Subtraction operator (-)
  • Multiplication operator (*)
  • Division operator (/)
  • Modulus operator (%)

Which is also called as Mathematical Operator

Syntax

Addition Operator

var variable_one = variable_two + variable_three

For example,

var sum = 1+2

Subtraction Operator

var variable_one = variable_two - variable_three

For example,

var sub = 10-2

Multiplication Operator

var variable_one = variable_two * variable_three

For example,

var mul = 10*2

Division Operator

var variable_one = variable_two / variable_three

For example,

var div = 10/2

Modulus Operator

var variable_one = variable_two % variable_three

For example,

var mod = 21%4

Arithmetic Operator Example Program in Kotlin

//Arithmetic Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program

fun main(args: Array<String>) {
    val num1 = 100;
    val num2 = 5

    //Addition
    val addition = num1+num2
    //Subtraction
    val subtraction = num1-num2
    //Multiplication
    val multiplication = num1*num2
    //Division
    val division = num1/num2
    //Modulus
    val modulus = num1%num2

    println("Addition of $num1 and $num2 is : $addition")
    println("Subtraction of $num1 and $num2 is : $subtraction")
    println("Multiplication of $num1 and $num2 is : $multiplication")
    println("Division of $num1 and $num2 is : $division")
    println("Modulus of $num1 and $num2 is : $modulus")
}

Sample Output

Addition of 100 and 5 is : 105
Subtraction of 100 and 5 is : 95
Multiplication of 100 and 5 is : 500
Division of 100 and 5 is : 20
Modulus of 100 and 5 is : 0