5 ways to swap two numbers in Kotlin

How to swap two numbers in Kotlin:

In this post, we learn different ways to swap two numbers in Kotlin. We can use a third variable to swap two numbers or we can use a basic mathematical formula to swap the numbers. Kotlin also provides different scope functions that can also be used to swap the numbers easily.

Method 1: How to swap two numbers in Kotlin by using a third variable:

We can use a third variable to swap two numbers in Kotlin. We can use the below algorithm to swap two numbers with a third variable:

  • Take the numbers as inputs from the user and assign them to two different variables.
  • Assign the value of the second variable to a third variable.
  • Assign the value of the first variable to the second variable.
  • Assign the value of the third variable to the first variable.

For example, if the user entered 100 and 200 as the first and the second numbers. Suppose, a is the first variable and b is the second variable. a = 100 and b = 200.

  1. Assign the value of b to a third variable c. a = 100, b = 200, c = 200
  2. Assign the value of a to b. a = 100, b = 100, c = 200
  3. Assign the value of c to a. a = 200, b = 100, c = 200

So, before swapping the values, variable a was 100 and b was 200. After swapping, variable a becomes 200 and b becomes 100.

Let’s write down the complete program:

fun main() {
    print("Enter the first number: ")
    var firstNumber = readlnOrNull()?.toInt()

    print("Enter the second number: ")
    var secondNumber = readlnOrNull()?.toInt()

    println("First Number: $firstNumber, Second Number: $secondNumber")

    val tempVariable = secondNumber
    secondNumber = firstNumber
    firstNumber = tempVariable

    println("After swapping,\nFirst Number: $firstNumber, Second Number: $secondNumber")
}

Here,

  • The firstNumber and secondNumber variables are used to hold the first and the second number. It asks the user to enter the numbers and assign the numbers to these variables.
  • It creates a third variable called tempVariable to hold the value of the secondNumber temporarily.
  • It is using the same algorithm to swap the numbers we discussed above.

If you run this program, it will work as below:

Enter the first number: 124
Enter the second number: 455
First Number: 124, Second Number: 455
After swapping,
First Number: 455, Second Number: 124

Method 2: Kotlin program to swap two numbers without using a third variable:

We can also swap two numbers without using an extra variable. It is easy! For example, if a and b are two numbers, we can use the below algorithm to swap them:

a = a - b
b = a + b
a = b - a

These three lines will swap the values stored in a and b. Suppose, a is equal to 10 and b is equal to 30. Now,

  • a = a - b, i.e. a = 10 - 30 = -20
  • b = a + b, i.e. b = -20 + 30 = 10
  • a = b - a, i.e. a = 10 - (-20) = 10 + 20 = 30

As you can see, the values of a and b are swapped. Below is the complete program:

fun main() {
    print("Enter the first number: ")
    var firstNumber = readln().toInt()

    print("Enter the second number: ")
    var secondNumber = readln().toInt()

    println("First Number: $firstNumber, Second Number: $secondNumber")

    firstNumber -= secondNumber
    secondNumber += firstNumber
    firstNumber = secondNumber - firstNumber

    println("After swapping,\nFirst Number: $firstNumber, Second Number: $secondNumber")
}

Sample output:

Enter the first number: 30
Enter the second number: 23
First Number: 30, Second Number: 23
After swapping,
First Number: 23, Second Number: 30

Method 3: By using Kotlin also scope function:

The also scope function can be used to swap the values of two numbers.

a = b.also{ b = a }

The return value of also is the context object itself. It will first assign the value of a to b. Then it will return the original value of b and it will be assigned to a.

fun main() {
    print("Enter the first number: ")
    var firstNumber = readln().toInt()

    print("Enter the second number: ")
    var secondNumber = readln().toInt()

    println("First Number: $firstNumber, Second Number: $secondNumber")

    firstNumber = secondNumber.also { secondNumber = firstNumber }

    println("After swapping,\nFirst Number: $firstNumber, Second Number: $secondNumber")
}

Kotlin example swap two numbers

Method 4: By using Kotlin with scope function:

The context object is passed as an argument to the with function. It is available as this inside the lambda. We can use with to swap two numbers as below:

with(a){
	a = b
	b = this
}

It will first assign the value of b to a. Since the value of a is passed as this, it is assigned to b on the second statement.

fun main() {
    print("Enter the first number: ")
    var firstNumber = readln().toInt()

    print("Enter the second number: ")
    var secondNumber = readln().toInt()

    println("First Number: $firstNumber, Second Number: $secondNumber")

    with(firstNumber){
        firstNumber = secondNumber
        secondNumber = this
    }

    println("After swapping,\nFirst Number: $firstNumber, Second Number: $secondNumber")
}

Method 5: By using Kotlin apply scope function:

The apply is another scope function that we can use to swap two numbers in Kotlin. This function is used mainly to operate with the members of the receiver object and it doesn’t return any value.

We can use it as like below to swap two numbers a and b:

a = b.apply{b = a}

Below is the complete program:

fun main() {
    print("Enter the first number: ")
    var firstNumber = readln().toInt()

    print("Enter the second number: ")
    var secondNumber = readln().toInt()

    println("First Number: $firstNumber, Second Number: $secondNumber")

    firstNumber = secondNumber.apply {
        secondNumber = firstNumber
    }

    println("After swapping,\nFirst Number: $firstNumber, Second Number: $secondNumber")
}

It will print a similar result.

Enter the first number: 445
Enter the second number: 998
First Number: 445, Second Number: 998
After swapping,
First Number: 998, Second Number: 445

You might also like: