Given two numbers a and b, the task is to swap them.
Examples:
Input: a = 2, b = 3
Output: 3 2Input: a = 20, b = 0
Output: 0 20Input: a = 10, b = 10
Output: 10 10
Try It Yourself
Table of Content
Using a Third Variable
The idea is to use a third variable, say temp to store the original value of one of the variables during the swap.
Let us understand with an example, a = 10, b = 20
- Store the value of a in temp, temp = 10
- Assign the value of b to a, a = 20
- Assign the value of temp to b, b = 10
// C++ Code to swap two numbers using third variable
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
cout << a << " " << b << endl;
return 0;
}
// C Code to swap two numbers using third variable
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
return 0;
}
// Java Code to swap two numbers using third variable
public class Main {
public static void main(String[] args) {
int a = 10, b = 20;
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
System.out.println(a + " " + b);
}
}
# Python3 Code to swap two numbers using third variable
a = 10
b = 20
# Swap a and b using temp variable
temp = a
a = b
b = temp
print(a, b)
// C# Code to swap two numbers using third variable
using System;
class Program {
static void Main() {
int a = 10, b = 20;
// Swap a and b using temp variable
int temp = a;
a = b;
b = temp;
Console.WriteLine(a + " " + b);
}
}
// JavaScript Code to swap two numbers using third variable
let a = 10, b = 20;
// Swap a and b using temp variable
let temp = a;
a = b;
b = temp;
console.log(a + ' '+ b);
Output
20 10
Without a Third Variable – Using Arithmetic Operations
The idea is to store sum in a variable so that we can get both values without third variable.
Let us understand with an example, a = 10, b = 20
- Store the sum of a and b in a, a = a + b = 10 + 20 = 30
- Get the original value of a and store it in b, b = a - b = 30 - 20 = 10
- Get the original value of b and store it in a, a = a - b = 30 - 10 = 20
- Final result: a = 20, b = 10
// C++ Code to swap two numbers using arithmetic operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
cout << a << " " << b << endl;
return 0;
}
#include <stdio.h>
int main() {
int a = 10, b = 20;
// swap two numbers using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n", a, b);
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 10, b = 20;
//swap two numbers using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
System.out.println(a + " " + b);
}
}
a = 10
b = 20
#swap two numbers using arithmetic operators
a = a + b
b = a - b
a = a - b
print(a, b)
using System;
class Program {
static void Main() {
int a = 10, b = 20;
//swap two numbers using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine(a + " " + b);
}
}
let a = 10, b = 20;
//swap two numbers using arithmetic operators
a = a + b;
b = a - b;
a = a - b;
console.log(a + ' '+ b);
Output
20 10
Without a Third Variable – Using Bitwise XOR
The idea is to use the properties of XOR to swap the two variables.
Let us understand with an example, a = 10, b = 20
- Store the XOR of a and b in a, a = a ^ b = 10 ^ 20 = 30
- Get the original value of a and store it in b, b = a ^ b = 30 ^ 20 = 10
- Get the original value of b and store it in a, a = a ^ b = 30 ^ 10 = 20
- Final result: a = 20, b = 10
// C++ Code to swap two numbers using bitwise XOR
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << " " << b << endl;
return 0;
}
// C Code to swap two numbers using bitwise XOR
#include <stdio.h>
int main() {
int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d\n", a, b);
return 0;
}
// Java Code to swap two numbers using bitwise XOR
public class Main {
public static void main(String[] args) {
int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println(a + " " + b);
}
}
# Python3 Code to swap two numbers using bitwise XOR
a = 10
b = 20
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b)
// C# Code to swap two numbers using bitwise XOR
using System;
class Program {
static void Main() {
int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
Console.WriteLine(a + " " + b);
}
}
// JavaScript Code to swap two numbers using bitwise XOR
let a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log(a, b);
Output
20 10
Using Built-in Swap Methods
We can also swap using built-in functionalities like swap() function in C++, tuple unpacking in Python, destructuring assignment in JavaScript.
// C++ Code to swap two numbers using swap function
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
swap(a, b);
cout << a << " " << b << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d\n", a, b);
return 0;
}
def swap(a, b):
return b, a
a = 10
b = 20
a, b = swap(a, b)
print(a, b)
function swap(a, b) {
return [b, a];
}
let a = 10, b = 20;
[a, b] = swap(a, b);
console.log(a +' ' + b);
Output
20 10