Swap Two Numbers (original) (raw)

Last Updated : 19 Apr, 2026

Given two numbers **a and **b, the task is to swap them.

**Examples:

**Input: a = 2, b = 3
**Output: 3 2

**Input: a = 20, b = 0
**Output: 0 20

**Input: a = 10, b = 10
**Output: 10 10

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

// C++ Code to swap two numbers using third variable

#include 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

// 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

// 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);
}

}

Python

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#

// 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

// 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);

`

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

// C++ Code to swap two numbers using arithmetic operators

#include 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

#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;

}

Java

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);
}

}

Python

a = 10 b = 20

#swap two numbers using arithmetic operators a = a + b b = a - b a = a - b

print(a, b)

C#

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);
}

}

JavaScript

let a = 10, b = 20;

//swap two numbers using arithmetic operators a = a + b; b = a - b; a = a - b;

console.log(a + ' '+ b);

`

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

// C++ Code to swap two numbers using bitwise XOR #include 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

// 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

// 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);
}

}

Python

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#

// 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

// 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);

`

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++ `

// C++ Code to swap two numbers using swap function

#include using namespace std;

int main() { int a = 10, b = 20;

swap(a, b);

cout  << a << " " << b << endl;
return 0;

}

C

#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; }

Python

def swap(a, b): return b, a

a = 10 b = 20 a, b = swap(a, b) print(a, b)

JavaScript

function swap(a, b) { return [b, a]; }

let a = 10, b = 20; [a, b] = swap(a, b); console.log(a +' ' + b);

`