0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Swap Two Numbers

In Kotlin, you can swap two numbers using a temporary variable like this: val temp = a; a = b; b = temp.
📋

Examples

Inputa = 5, b = 10
OutputAfter swap: a = 10, b = 5
Inputa = -3, b = 7
OutputAfter swap: a = 7, b = -3
Inputa = 0, b = 0
OutputAfter swap: a = 0, b = 0
🧠

How to Think About It

To swap two numbers, think of holding one number temporarily so you don't lose it when you assign the other number to its place. Use a temporary container to keep one number, then replace it with the other, and finally put the saved number into the second variable.
📐

Algorithm

1
Get the two numbers to swap.
2
Store the first number in a temporary variable.
3
Assign the second number to the first variable.
4
Assign the temporary variable's value to the second variable.
5
Print or return the swapped values.
💻

Code

kotlin
fun main() {
    var a = 5
    var b = 10
    println("Before swap: a = $a, b = $b")
    val temp = a
    a = b
    b = temp
    println("After swap: a = $a, b = $b")
}
Output
Before swap: a = 5, b = 10 After swap: a = 10, b = 5
🔍

Dry Run

Let's trace swapping a = 5 and b = 10 through the code

1

Initial values

a = 5, b = 10

2

Store a in temp

temp = 5

3

Assign b to a

a = 10

4

Assign temp to b

b = 5

5

Final swapped values

a = 10, b = 5

Stepabtemp
Initial510-
Store a5105
Assign b to a10105
Assign temp to b1055
💡

Why This Works

Step 1: Use a temporary variable

We save the value of a in temp so it is not lost when we overwrite a.

Step 2: Assign second number to first

We put the value of b into a, replacing the original a.

Step 3: Restore saved value

We assign the saved value in temp to b, completing the swap.

🔄

Alternative Approaches

Using Kotlin's built-in destructuring
kotlin
fun main() {
    var a = 5
    var b = 10
    println("Before swap: a = $a, b = $b")
    a = b.also { b = a }
    println("After swap: a = $a, b = $b")
}
This uses Kotlin's <code>also</code> function to swap without a temporary variable explicitly, but it is less readable for beginners.
Using arithmetic operations
kotlin
fun main() {
    var a = 5
    var b = 10
    println("Before swap: a = $a, b = $b")
    a = a + b
    b = a - b
    a = a - b
    println("After swap: a = $a, b = $b")
}
This swaps without extra memory but can cause overflow and is less clear.

Complexity: O(1) time, O(1) space

Time Complexity

Swapping two numbers takes constant time because it involves a fixed number of steps without loops.

Space Complexity

Only a small temporary variable is used, so space is constant.

Which Approach is Fastest?

All approaches run in constant time; using a temporary variable is simplest and safest, while arithmetic methods save memory but risk errors.

ApproachTimeSpaceBest For
Temporary variableO(1)O(1)Clarity and safety
Kotlin also functionO(1)O(1)Concise Kotlin style
Arithmetic operationsO(1)O(1)Memory saving but risky
💡
Use a temporary variable to keep one number safe while swapping to avoid losing data.
⚠️
Forgetting to use a temporary variable causes one number to be overwritten and lost.