# How to swap two numbers in Kotlin

To Swap 2 numbers in Kotlin we can declare a third temporary variable.Assuming we have a = 1, b =2

And Temporary variable called x

1- We set value of temporary from first variable. x = 1 (a)

2- We set value of First variable from Second variable. a = 2 (b)

3- We set value of second variable from temporary variable b = 1 (x)

#### Example

```
import java.util.*
fun main(arg: Array<String>) {
var first = 1;
var second = 2;
println("Numbers Before Swap : first = $first second = $second ")
var temp = first
first = second
second = temp
println("Numbers After Swap : first = $first second = $second ")
}
```

### Output

Numbers Before Swap : first = 1 second = 2

Numbers After Swap : first = 2 second = 1

Numbers After Swap : first = 2 second = 1