Arithmetic in Kotlin
Kotlin provides a wide range of arithmetic operators and mathematical functions to perform various calculations. These include standard arithmetic operators as well as operator functions and mathematical functions. Below are the subtopics we'll cover
- Addition (+),
plus()function - Subtraction (-),
minus()function - Multiplication (*),
times()function - Division (/),
div()function - Modulus (%),
mod()function - Increment
inc() - Decrement
dec() - Mathematical Functions
Addition (+), plus() function: Addition can be performed using the + operator or the plus() function. Both work the same way, adding two numbers together.
Example:
fun main() {
val a = 5
val b = 3
val sum1 = a + b // Using + operator
val sum2 = a.plus(b) // Using plus() function
println("Sum using + operator: $sum1") // Output: Sum using + operator: 8
println("Sum using plus(): $sum2") // Output: Sum using plus(): 8
}
Subtraction (-), minus() function: Subtraction can be done using the - operator or the minus() function.
Example:
fun main() {
val a = 10
val b = 4
val difference1 = a - b // Using - operator
val difference2 = a.minus(b) // Using minus() function
println("Difference using - operator: $difference1") // Output: Difference: 6
println("Difference using minus(): $difference2") // Output: Difference: 6
}
Multiplication (*), times() function: Multiplication can be done using the * operator or the times() function.
Example:
fun main() {
val a = 7
val b = 3
val product1 = a * b // Using * operator
val product2 = a.times(b) // Using times() function
println("Product using * operator: $product1") // Output: Product: 21
println("Product using times(): $product2") // Output: Product: 21
}
Division (/), div() function: Division can be done using the / operator or the div() function. For integer division, the result will be an integer.
Example:
fun main() {
val a = 9
val b = 2
val quotient1 = a / b // Using / operator
val quotient2 = a.div(b) // Using div() function
println("Quotient using / operator: $quotient1") // Output: 4
println("Quotient using div(): $quotient2") // Output: 4
}
Modulus (%), mod() function: The modulus operator returns the remainder of a division. You can also use the mod() function.
Example:
fun main() {
val a = 10
val b = 3
val remainder1 = a % b // Using % operator
val remainder2 = a.mod(b) // Using mod() function
println("Remainder using % operator: $remainder1") // Output: 1
println("Remainder using mod(): $remainder2") // Output: 1
}
Increment inc(): The inc() function increments a value by 1.
Example:
fun main() {
var a = 5
a = a.inc() // Incrementing by 1
println("Incremented value: $a") // Output: 6
// or use ++ for increment
var b = 2
b ++
println("Incremented value: $b") // output 3
}
Decrement dec(): The dec() function decrements a value by 1.
Example:
fun main() {
var a = 10
a = a.dec() // Decrementing by 1
println("Decremented value: $a") // Output: 9
// or use -- for Decremented
var b = 2
b --
println("Decremented value: $b") // output 1
}
Mathematical Functions
Kotlin provides several built-in functions for common mathematical calculations, including:
abs()– Returns the absolute value of a number.ceil()– Rounds a floating-point number up to the nearest whole number.floor()– Rounds a floating-point number down to the nearest whole number.round()– Rounds a floating-point number to the nearest integer.sqrt()– Returns the square root of a number.pow()– Raises a number to the power of another number.min()– Returns the smaller of two numbers.max()– Returns the larger of two numbers.
Example of Mathematical Functions:
fun main() {
val number = -5
println("Absolute value: ${abs(number)}") // Output: 5
val num = 4.7
println("Ceiling: ${ceil(num)}") // Output: 5.0
println("Floor: ${floor(num)}") // Output: 4.0
println("Square root of 16: ${sqrt(16.0)}") // Output: 4.0
println("2 raised to the power of 3: ${2.0.pow(3)}") // Output: 8.0
}
Summary of Arithmetic Operators and Functions
- Arithmetic operators include +, -, *, /, %, and their function equivalents like
plus(),minus(),times(), etc. - Increment (
inc()) and decrement (dec()) allow easy addition and subtraction of 1. - Mathematical functions like
abs(),sqrt(),pow(), and others offer more complex calculations.
