How to swap two numbers without using temporary variable?

Write a program to swap or exchange two numbers. You should not use any temporary or third variable to swap. Kotlin program to swap two numbers without using temporary variable


fun main(a: Array<String>) {
    var x = 10
    var y = 20
    println("Before swap:")
    println("x value: $x")
    println("y value: $y")
    x = x + y
    y = x - y
    x = x - y
    println("After swap:")
    println("x value: $x")
    println("y value: $y")
}


Output


Before swap:
x value: 10
y value: 20
After swap:
x value: 20
y value: 10

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions