Subtraction is one of the most basic and important operations in programming. In Kotlin, subtracting numbers helps beginners understand how values change, how calculations work, and how programs produce results. Even though subtraction looks simple, it is used everywhere, from checking balances to calculating scores and tracking remaining values.
Kotlin follows a clean and traditional style of programming, which makes subtraction easy to read and easy to learn. By understanding how to subtract numbers in Kotlin, you build confidence to move on to more complex logic. This guide explains subtraction step by step using clear examples that feel natural and practical for beginners.
Program 1: Subtracting Two Integers in Kotlin
This program shows how to subtract one whole number from another using simple predefined values. It is the best place to start when learning subtraction in Kotlin.
fun main() {
val totalCats = 10
val catsGone = 4
val catsLeft = totalCats - catsGone
println("Cats left: $catsLeft")
}In this program, Kotlin subtracts one integer from another using the minus symbol. The result is stored in a new variable and printed to the screen. Beginners can easily understand how subtraction works by reading the variable names and following the flow of the program.
Program 2: Subtracting Floating-Point Numbers
This program demonstrates subtraction using decimal numbers, which are common in real-world situations like money or measurements.
fun main() {
val movieLength = 2.5
val watchedTime = 1.75
val remainingTime = movieLength - watchedTime
println("Remaining time: $remainingTime hours")
}Kotlin handles decimal subtraction smoothly without extra steps. This is useful when working with prices, time, or distances. Beginners learn that subtraction works the same way for decimals as it does for whole numbers.
Program 3: Subtracting an Integer from a Float
This program shows how Kotlin subtracts numbers when they are of different types, such as a whole number and a decimal.
fun main() {
val totalDistance = 15.5
val distanceCovered = 6
val distanceLeft = totalDistance - distanceCovered
println("Distance left: $distanceLeft km")
}Kotlin automatically converts the integer into a decimal during the calculation. This makes the language forgiving and beginner-friendly. You can focus on the logic instead of worrying about number conversions.
Program 4: Subtracting Numbers Using a Function
This program uses a function to perform subtraction, which is a classic and clean programming approach.
fun subtractNumbers(firstValue: Int, secondValue: Int): Int {
return firstValue - secondValue
}
fun main() {
val result = subtractNumbers(20, 7)
println("Result: $result")
}Functions help organize code and make it reusable. Even though subtraction is simple, placing it in a function teaches good habits early. Beginners can later reuse this function in larger programs without rewriting logic.
Program 5: Subtracting Numbers Entered by the User
This program allows the user to enter two numbers and subtracts the second from the first.
fun main() {
print("Enter the first number: ")
val firstInput = readLine()!!.toInt()
print("Enter the second number: ")
val secondInput = readLine()!!.toInt()
val result = firstInput - secondInput
println("The result is: $result")
}Here, Kotlin reads input from the user and converts it into numbers before subtracting. This example shows how subtraction is used in interactive programs. Beginners can see how real applications work with user data.
Program 6: Subtracting Values with Clear Meaning
This program focuses on clarity by using variable names that describe a real situation.
fun main() {
val dogsAtStart = 12
val dogsAdopted = 5
val dogsRemaining = dogsAtStart - dogsAdopted
println("Dogs remaining: $dogsRemaining")
}Meaningful variable names make subtraction easier to understand. This traditional style has always been encouraged in programming. Beginners learn that clear code is better than clever code.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about subtracting numbers in Kotlin and clears up simple doubts.
Q1. Which symbol is used for subtraction in Kotlin?
Kotlin uses the minus symbol to subtract numbers.
Q2. Can Kotlin subtract decimal numbers?
Yes, Kotlin fully supports decimal subtraction.
Q3. What happens when subtracting different number types?
Kotlin automatically handles type conversion when needed.
Q4. Why use functions for subtraction?
Functions make code reusable and easier to maintain.
Q5. Is subtraction in Kotlin hard for beginners?
No, Kotlin keeps subtraction simple and readable.
Conclusion
Subtracting numbers in Kotlin is simple, clear, and beginner-friendly. In this article, you learned how to subtract integers, decimals, mixed values, and user input, as well as how to organize subtraction logic using functions. Each example showed how Kotlin keeps calculations clean and easy to understand.
The best way to master subtraction is through practice. Try changing the numbers, writing your own examples, and experimenting with user input. With time and repetition, subtraction will feel natural and help you move confidently toward more advanced Kotlin programming.




