Arithmetic Operators in Kotlin (Mathematical Operators)

Welcome back to Kotlin Operators tutorials, In this tutorial, we will discuss all the basic Arithmetic Operators in Kotlin, we also called it (Mathematical Operators).

Arithmetical operators are making a calculation between two variables like addition, subtraction, multiplication and division.

⦿ Arithmetic Operators in Kotlin (Mathematical Operators)

In Kotlin there are 5 types of Arithmetical operators:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Modulus

Operator & Name

Operator

+
*
/
%

Name

Addition Operator
Subtraction Operator
Multiplication Operator
Division Operator
Modulus Operator

Note: + Operator is also used for string concatenation

Example of String concatenation:

fun main() {
val first = "Hello "
val last = "TechpassMaster"
val addFirstAndLast = first + last
println(addFirstAndLast)
}

When you run the program, you will get the output:

Hello TechpassMaster

⦿ Use of Arithmetic Operators (Mathematical Operators)

Suppose you have to number for Addition, Subtraction, Multiplication, Division, and Modulus two numbers, so in the case, you will use Arithmetic Operators (Mathematical Operators), let see how we can use these operators.

1. Addition operator (+)

Variable: addTwoNumbers
Operand: 50
Operator: +
Operand: 50

Variable Operand Operator Operand
addTwoNumbers 50 + 50

The basic program for Add two Numbers:

fun main() {
// var variableName= firstOprend + SecondOprend

// For example:

var addTwoNumbers= 50 + 50
println(addTwoNumbers)
}

Output:

100

2. Subtraction operator (-)

Variable : subtractTwoNumbers
Operand : 50
Operator : –
Operand : 50

Variable Operand Operator Operand
subtractTwoNumbers 50 50

The basic program for subtraction of two numbers:

fun main() {
// var variableName= firstOprend - SecondOprend

// For example:

var subtractTwoNumbers = 50 - 50
println(subtractTwoNumbers)
}

Output:

0

The syntax for Multiply two Numbers ( Multiplication operator (*) )

3. Multiplication operator (*)

Variable : multuplyTwoNumbers
Operand : 50
Operator : *
Operand : 50

Variable Operand Operator Operand
multuplyTwoNumbers 50 * 50

The basic program for Multiply of two numbers:

fun main() {
// var variableName= firstOprend * SecondOprend

// For example:

var multuplyTwoNumbers = 50 * 50
println(multuplyTwoNumbers)
}

Output:

2500

4. Division Operator (/)

Variable : divideTwoNumbers
Operand : 50
Operator : /
Operand : 50

Variable Operand Operator Operand
divideTwoNumbers 50 / 50

The basic program for division of two numbers:

fun main() {
// var variableName= firstOprend / SecondOprend

// For example:

var divideTwoNumbers = 50 / 50
println(multuplyTwoNumbers)
}

Run the program, the output will be:

2500

5. Modulus Operator (%)

Variable: modulesTwoNumbers
Operand
: 50
Operator: %
Operand: 50

Variable Operand Operator Operand
modulesTwoNumbers 50 % 50

The basic program for modules of two numbers:

fun main() {
// var variableName= firstOprend % SecondOprend

// For example:

var modulesTwoNumbers = 50 % 50
println(divideTwoNumbers)
}

Output:

1
fun main() {
// var variableName= firstOprend % SecondOprend

// For example:

var modulusTwoNumbers = 50 % 50
println(modulusTwoNumbers)
}

Run the program and output will be:

0

Read more:

⦿ Example: Arithmetic Operators (Mathematical Operators) Program in Kotlin

fun main() {
var firstNumber = 50
var secondNumber = 50

// Addition operator (+)
var addTwoNumbers= firstNumber + secondNumber
println(addTwoNumbers)

// Subtraction operator (-)
var subtractTwoNumbers= firstNumber - secondNumber
println(subtractTwoNumbers)

// Multiplication operator (*)
var multiplyTwoNumbers= firstNumber * secondNumber
println(multiplyTwoNumbers)

// Division operator (/)
var divideTwoNumbers= firstNumber / secondNumber
println(divideTwoNumbers)

// Modulus operator (%)
var modulusTwoNumbers= firstNumber % secondNumber
println(modulusTwoNumbers)
}

When you run the program, the output will be:

100
0
2500
1
0

If you have any questions regarding this post, feel free to comment and share the post with other developers, who want to learn Kotlin and Android App development.

Happy Learning!!!

Read Next
Click Here ⇝ Relational Comparison Operators in Kotlin

Leave a Reply

Your email address will not be published. Required fields are marked *