Arithmetic Operators (Mathematical Operators) in Kotlin
On this page (11sections)
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
How It Works
Here is a step-by-step walkthrough of how the Kotlin program for Arithmetic Operators (Mathematical Operators) in Kotlin runs, line by line:
fun main(args: Array<String>) {— defines a function used by the program.val num1 = 100;— declares or assigns a value the program uses.val num2 = 5— declares or assigns a value the program uses.val addition = num1+num2— declares or assigns a value the program uses.val subtraction = num1-num2— declares or assigns a value the program uses.val multiplication = num1*num2— declares or assigns a value the program uses.val division = num1/num2— declares or assigns a value the program uses.val modulus = num1%num2— declares or assigns a value the program uses.
After running it, compare your console output with the Sample Output above. Try changing the values and re-running the program to see how the result changes — experimenting is the fastest way to understand the logic.
Frequently Asked Questions
What is Arithmetic Operators (Mathematical Operators) in Kotlin?
Arithmetic Operators (Mathematical Operators) is demonstrated in this Kotlin tutorial with a complete, runnable example and its sample output.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Arithmetic Operators (Mathematical Operators)?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.