Welcome back to Kotlin Operators tutorials, In this post, we will discuss about Arithmetic Operators (Mathematical Operators) in Kotlin.
Page Contents
⦿ Arithmetic Operators (Mathematical Operators) in Kotlin
In Kotlin there are 5 types of Artimaticle operators, which are also called Mathematical Operators.
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 Muliply 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 Muliply 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
Recommended Reading:
⦿ 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
Recommended Reading:
- What Is Kotlin In Android.
- Basic Syntax Of Kotlin.
- Android Interview Questions For Fresher.
- Top 5 Things To Avoid While Developing Android Apps.
- How To Make An Android App For Beginners
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!!!