Last Updated : 23 Jul, 2025
Try it on GfG Practice
Given two variables a and y, swap two variables without using a third variable.
Examples:
Using Arithmetic OperatorsInput: a = 2, b = 3
Output: a = 3, b = 2Input: a = 20, b = 0
Output: a = 0, b = 20Input: a = 10, b = 10
Output: a = 10, b = 10
// 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);
a = 2 b = 3 a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
The idea is to use the properties of XOR to swap the two variables.
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);
a = 2 b = 3 a = 3 b = 2
Time Complexity: O(1)
Auxiliary Space: O(1)
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 NumbersRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4