Swap Two Numbers Without Using Third Variable
Last Updated :
23 Jul, 2025
Given two variables a and y, swap two variables without using a third variable.
Examples:
Input: a = 2, b = 3
Output: a = 3, b = 2
Input: a = 20, b = 0
Output: a = 0, b = 20
Input: a = 10, b = 10
Output: a = 10, b = 10
Using Arithmetic Operators
- Store the sum of a and b in a (a = a + b).
- Get the original value of a, that is (sum - original value of b)and store it in b (b = a - b).
- Get the original value of b, that is (sum - original value of a)and store it in a (a = a - b).
C++
// C++ Code to swap two numbers using arithmetic operators
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3;
cout << "a = " << a << " b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "a = " << a << " b = " << b << endl;
return 0;
}
C
// C Code to swap two numbers using arithmetic operators
#include <stdio.h>
int main() {
int a = 2, b = 3;
printf("a = %d b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d b = %d\n", a, b);
return 0;
}
Java
// Java Code to swap two numbers using arithmetic operators
class GfG {
public static void main(String[] args) {
int a = 2, b = 3;
System.out.println("a = " + a + " b = " + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + " b = " + b);
}
}
Python
# Python Code to swap two numbers using arithmetic operators
if __name__ == "__main__":
a = 2
b = 3
print("a =", a, " b =", b)
a = a + b
b = a - b
a = a - b
print("a =", a, " b =", b)
C#
// C# Code to swap two numbers using arithmetic operators
using System;
class GfG {
static void Main() {
int a = 2, b = 3;
Console.WriteLine("a = " + a + " b = " + b);
// Swap a and b using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine("a = " + a + " b = " + b);
}
}
JavaScript
// JavaScript Code to swap two numbers using arithmetic operators
let a = 2, b = 3;
console.log("a = " + a + " b = " + b);
a = a + b;
b = a - b;
a = a - b;
console.log("a = " + a + " b = " + b);
Outputa = 2 b = 3
a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Using Bitwise XOR
The idea is to use the properties of XOR to swap the two variables.
- a = a ^ b: Store the Bitwise XOR of a and b in a. Now, a holds the result of (a ^ b).
- b = a ^ b: Bitwise XOR the new value of a with b to get the original value of a. This gives us, b = (a ^ b) ^ b = a.
- a = a ^ b: Bitwise XOR the new value of a with the new value of b (which is the original a) to get the original value of b. This gives us, a = (a ^ b) ^ a = b.
Finally, a and b hold the swapped values.
C++
// C++ Code to swap two numbers using bitwise XOR
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3;
cout << "a = " << a << " b = " << b << endl;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << "a = " << a << " b = " << b << endl;
return 0;
}
C
// C Code to swap two numbers using bitwise XOR
#include <stdio.h>
int main() {
int a = 2, b = 3;
printf("a = %d b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a = %d b = %d\n", a, b);
return 0;
}
Java
// Java Code to swap two numbers using bitwise XOR
class GfG {
public static void main(String[] args) {
int a = 2, b = 3;
System.out.println("a = " + a + " b = " + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a = " + a + " b = " + b);
}
}
Python
# Python Code to swap two numbers using bitwise XOR
if __name__ == "__main__":
a = 2
b = 3
print("a =", a, " b =", b)
a = a ^ b
b = a ^ b
a = a ^ b
print("a =", a, " b =", b)
C#
// C# Code to swap two numbers using bitwise XOR
using System;
class GfG {
static void Main() {
int a = 2, b = 3;
Console.WriteLine("a = " + a + " b = " + b);
// Swap a and b using arithmetic operators
a = a ^ b;
b = a ^ b;
a = a ^ b;
Console.WriteLine("a = " + a + " b = " + b);
}
}
JavaScript
// JavaScript Code to swap two numbers using bitwise XOR
let a = 2, b = 3;
console.log("a = " + a + " b = " + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log("a = " + a + " b = " + b);
Outputa = 2 b = 3
a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Built-in Swap
We can also swap using built-in functionalities like swap method in C++, tuple unpacking in Python, destructuring assignment in JavaScript. To know more about the implementation, please refer Swap Two Numbers.
C++ Program to Swap 2 Numbers
C++ Program to Swap 2 Numbers
C Program to Swap 2 Numbers without using a Temporary Variable
JavaScript Program to Swap Two Numbers
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem