In this post, we will see examples on how to swap two numbers using a temporary variable and without using a temporary variable.
Understanding the approach of Swapping Two Numbers
Imagine you have red marbles in green glass and green marbles in red glass. How do you interchange the marbles so that red marbles will be in red glass and green marbles will be in green glass?
One way is to take one more empty white glass and pour the green marbles of red glass into it. Pour the red marbles from green glass into red glass and then pour the green marbles from white glass into green glass.
Another way would be to first pour red marbles of green glass into red glass. Now red glass will have both red and green marbles. Then remove green marbles from the red glass and place it in green glass.
Similar to the above analogy, we can swap two numbers in two ways.
- Using temporary variable
- Without using temporary variable
Using Temporary Variable
First way is to store one of the variable value, say A’s value in another memory location, a temporary third variable T. Then copy B’s value into A and then T’s value into A. In this approach we need an additional storage to store the value of a number.
A = 5
B = 10
T = A // A=5, B=10, T=5
A = B // A=10, B=10, T=5
B = T // A=10, B=5, T=5
Pseudocode
STEP 1: READ A, B
STEP 2: T = A
STEP 3: A = B
STEP 4: B = T
STEP 5: PRINT A, B
Flowchart
Without Using Temporary Variable
Second way is to add both A and B, and store the result in A. Now A has both A’s value and B’s value. If I want only A’s value, I can subtract B from A. I’ll store this result in B. We’ve solved half of the problem. Now, we need B’s value in A. For that, we can now subtract B’s value (which actually has A’s value) from A, with this B’s value will remain in A.
A = 5
B = 10
A = A + B // A=15, B=10
B = A - B // A=15, B=5
A = A - B // A=10, B=5
Pseudocode
STEP 1: READ A, B
STEP 2: A = A + B
STEP 3: B = A - B
STEP 4: A = A - B
STEP 5: PRINT A, B
Flowchart
Swap Two Numbers in Java
Using a Temporary Variable
package com.techstackjournal;
public class SwapNumbersExample1 {
public static void main(String[] args) {
int a = 5;
int b = 10;
int t;
t = a;
a = b;
b = t;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Without Using the Temporary Variable
package com.techstackjournal;
public class SwapNumbersExample2 {
public static void main(String[] args) {
int a = 5;
int b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Using Bitwise Operators
package com.techstackjournal.java.basics;
public class SwapNumbersExample3 {
public static void main(String[] args) {
int a = 5;
int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Swap Two Numbers in C language
Using a Temporary Variable:
#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
int t;
t = a;
a = b;
b = t;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}
Without Using Temporary Variable
#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}
Using Pointers within a Function
#include <stdio.h>
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int a = 5;
int b = 10;
swap(&a,&b);
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}
Using Bitwise Operators
#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}
Swap Two Numbers in Python
Using a Temporary Variable
a = 5;
b = 10;
t = a
a = b
b = t
print ("a = ", a)
print ("b = ", b)
Without Using Temporary Variable
a = 5;
b = 10;
a = a + b
b = a - b
a = a - b
print ("a = ", a)
print ("b = ", b)
Using Bitwise Operators
a = 5;
b = 10;
a = a ^ b
b = a ^ b
a = a ^ b
print ("a = ", a)
print ("b = ", b)