In this program, we will learn to code the Kotlin Program to Swap Two Numbers. Let’s understand How to Swap Two Numbers using Kotlin Programming Language.
Writing a Number Swapping Program in Kotlin Programming language is good for logic building. You get to know different aspects of logic building and how to implement them in your program to get the desired output.
Kotlin Program to Swap Two Numbers
Method 1 : using third variable
// Kotlin Program to Swap Two Numbers // Method 1 : using third variable fun main() { var num1 = 18 var num2 = 7 //Numbers before Swapping println("Before Swapping =>") println("first number is $num1") println("second number is $num2") //Swapping number var temp = num1 num1 = num2 num2 = temp println("\n=======================\n") //Numbers after Swapping println("After Swapping =>") println("first number is $num1") println("second number is $num2") }
Output
Before Swapping =>
first number is 18
second number is 7
=======================
After Swapping =>
first number is 7
second number is 18
How Does This Program Work?
//Swapping number var temp = num1 num1 = num2 num2 = temp
In the above code, the temp variable is assigned the value of num1
. Then, the value of num2
is assigned to the num1
. And finally, the temp variable( which holds the value of num1
) is assigned to num2
.
For example,num1
= 10 and num2
= 20
then we do, temp = num1
i.e. temp = 10
then, num1
= num2
i.e. num1
= 20 (now)
then, num1
= temp i.e. num1
= 10
Here, we don’t assign the value of num1
to num2
because the value of num1
has been changed to that of num2
that’s why we need a temp variable to store the value of num1
variable so that the value doesn’t get lost.
Now, let’s see the Kotlin Program to swap two numbers without using any third variable (temp)
Kotlin Program to Swap Two Numbers 2
Method 2 : without using any third variable
// Kotlin Program to Swap Two Numbers // Method 1 : using third variable fun main() { var num1 = 18 var num2 = 7 //Numbers before Swapping println("Before Swapping =>") println("first number is $num1") println("second number is $num2") //Swapping number num1 = num1 + num2 num2 = num1 - num2 num1 = num1 - num2 println("\n=======================\n") //Numbers after Swapping println("After Swapping =>") println("first number is $num1") println("second number is $num2") }
Output
Before Swapping =>
first number is 18
second number is 7
=======================
After Swapping =>
first number is 7
second number is 18
How Does This Program Work?
//Swapping number num1 = num1 + num2 num2 = num1 - num2 num1 = num1 - num2
In the above logic, we have used a basic arithmetic calculation to swap the numbers.
Let’s see that using an example will make it easy to understand the logic.
Suppose, a = 10 and b = 20
a = a + b = 10 + 20 = 30
b = a – b = 30 – 20 = 10
a = a – b = 30 – 10 = 20
Hence, the values are swapped with each other.
Conclusion
I hope after going through this post, you understand how to code Kotlin Program to Swap Two Numbers using 2 different methods. We swapped the number with both using the third variable and without using the third variable.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Learn More:
- Kotlin Program to Add Two Numbers
- Kotlin Program to Print an Integer Entered by the User
- Kotlin Program to Calculate Average Using Arrays
- Kotlin Program to Find Ascii Value of a Character
- Kotlin Program to Generate Multiplication Table