How to swap two numbers without using a temporary variable?

Given two variables, x and y, swap two variables without using a third variable.

Method 1 (Using Arithmetic Operators)
The idea is to get sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to swap two numbers  without
// using temporary variable
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int x = 10, y = 5;
  
    // Code to swap 'x' and 'y'
    x = x + y; // x now becomes 15
    y = x - y; // y becomes 10
    x = x - y; // x becomes 5
    cout << "After Swapping: x =" << x << ", y=" << y;
}
  
// This code is contributed by mohit kumar.

chevron_right


C

#include
int main()
{
int x = 10, y = 5;



// Code to swap ‘x’ and ‘y’
x = x + y; // x now becomes 15
y = x – y; // y becomes 10
x = x – y; // x becomes 5

printf(“After Swapping: x = %d, y = %d”, x, y);

return 0;
}

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to swap two numbers  without
// using temporary variable
import java.*;
  
class Geeks {
  
    public static void main(String a[])
    {
        int x = 10;
        int y = 5;
        x = x + y;
        y = x - y;
        x = x - y;
        System.out.println("After swaping:"
                           + " x = " + x + ", y = " + y);
    }
}
  
// This code is contributed by Sam007

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

x = 10
y = 5
   
# Code to swap 'x' and 'y'
  
# x now becomes 15
x = x + y  
  
# y becomes 10
y = x -
  
# x becomes 5
x = x - y  
print("After Swapping: x =", x, " y =", y);
  
# This code is contributed 
# by Sumit Sudhakar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to swap two numbers  without
// using temporary variable
using System;
  
class GFG {
    public static void Main()
    {
        int x = 10;
        int y = 5;
  
        x = x + y;
        y = x - y;
        x = x - y;
        Console.WriteLine("After swapping: x = "
                          + x + ", y = " + y);
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to swap two 
// numbers without using 
// temporary variable
$x = 10; $y = 5;
  
// Code to swap 'x' and 'y'
$x = $x + $y; // x now becomes 15
$y = $x - $y; // y becomes 10
$x = $x - $y; // x becomes 5
  
echo "After Swapping: x = "
       $x, ", ", "y = ", $y;
  
// This code is contributed by m_kit
?>

chevron_right


Output :



After Swapping: x = 5, y = 10

Multiplication and division can also be used for swapping.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to swap two numbers
// without using temporary variable
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int x = 10, y = 5;
  
    // Code to swap 'x' and 'y'
    x = x * y; // x now becomes 15
    y = x / y; // y becomes 10
    x = x / y; // x becomes 5
    cout << "After Swapping: x =" << x << ", y=" << y;
}
  
// This code is contributed by mohit kumar.

chevron_right


C

#include
int main()
{
int x = 10, y = 5;

// Code to swap ‘x’ and ‘y’
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5

printf(“After Swapping: x = %d, y = %d”, x, y);

return 0;
}

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to swap two numbers
// without using temporary variable
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        int x = 10;
        int y = 5;
  
        // Code to swap 'x' and 'y'
        x = x * y; // x now becomes 50
        y = x / y; // y becomes 10
        x = x / y; // x becomes 5
  
        System.out.println("After swaping:"
                           + " x = " + x + ", y = " + y);
    }
}
  
// This code is contributed by ajit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to 
# swap two numbers
# without using 
# temporary variable
x = 10
y = 5
  
# code to swap 
# 'x' and 'y'
  
# x now becomes 50
x = x * y
  
# y becomes 10
y = x // y; 
  
# x becomes 5
x = x // y; 
  
print("After Swapping: x ="
              x, " y =", y);
  
# This code is contributed 
# by @ajit

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to swap two
// numbers without using
// temporary variable
using System;
  
class GFG {
    static public void Main()
    {
        int x = 10;
        int y = 5;
  
        // Code to swap 'x' and 'y'
        x = x * y; // x now becomes 50
        y = x / y; // y becomes 10
        x = x / y; // x becomes 5
  
        Console.WriteLine("After swaping:"
                          + " x = " + x + ", y = " + y);
    }
}
  
// This code is contributed by ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Driver code
    $x = 10;
    $y = 5;
  
// Code to swap 'x' and 'y'
    $x = $x * $y; // x now becomes 50
    $y = $x / $y; // y becomes 10
    $x = $x / $y; // x becomes 5
  
echo "After Swapping: x = ", $x,
                " ", "y = ", $y;
  
// This code is contributed by m_kit
?>

chevron_right


Output :

After Swapping: x = 5, y = 10

Method 2 (Using Bitwise XOR)
The bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to swap using XOR
#include <bits/stdc++.h>
  
using namespace std;
  
int main()
{
    int x = 10, y = 5;
    // Code to swap 'x' (1010) and 'y' (0101)
    x = x ^ y; // x now becomes 15 (1111)
    y = x ^ y; // y becomes 10 (1010)
    x = x ^ y; // x becomes 5 (0101)
    cout << "After Swapping: x =" << x << ", y=" << y;
    return 0;
}
  
// This code is contributed by mohit kumar.

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C code to swap using XOR
#include <stdio.h>
int main()
{
    int x = 10, y = 5;
  
    // Code to swap 'x' (1010) and 'y' (0101)
    x = x ^ y; // x now becomes 15 (1111)
    y = x ^ y; // y becomes 10 (1010)
    x = x ^ y; // x becomes 5 (0101)
  
    printf("After Swapping: x = %d, y = %d", x, y);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to swap using XOR
import java.*;
  
public class GFG {
  
    public static void main(String a[])
    {
        int x = 10;
        int y = 5;
  
        // Code to swap 'x' (1010) and 'y' (0101)
        x = x ^ y; // x now becomes 15 (1111)
        y = x ^ y; // y becomes 10 (1010)
        x = x ^ y; // x becomes 5 (0101)
  
        System.out.println("After swap: x = "
                           + x + ", y = " + y);
    }
}
  
// This code is contributed by Sam007.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to swap using XOR
  
x = 10
y = 5
  
# Code to swap 'x' and 'y'
x = x ^ y; # x now becomes 15 (1111)
y = x ^ y; # y becomes 10 (1010)
x = x ^ y; # x becomes 5 (0101)
  
print ("After Swapping: x = ", x, " y =", y)
  
# This code is contributed by
# Sumit Sudhakar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to swap using XOR
using System;
  
class GFG {
    public static void Main()
    {
        int x = 10;
        int y = 5;
  
        // Code to swap 'x' (1010)
        // and 'y' (0101)
  
        // x now becomes 15 (1111)
        x = x ^ y;
  
        // y becomes 10 (1010)
        y = x ^ y;
  
        // x becomes 5 (0101)
        x = x ^ y;
  
        Console.WriteLine("After swap: x = " + x + ", y = " + y);
    }
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Driver Code 
$x = 10;
$y = 5;
  
// Code to swap 'x' (1010) 
// and 'y' (0101)
  
// x now becomes 15 (1111)
$x = $x ^ $y
  
// y becomes 10 (1010)
$y = $x ^ $y
  
// x becomes 5 (0101)
$x = $x ^ $y
  
echo "After Swapping: x = ", $x
                ", ", "y = ", $y;
  
// This code is contributed by aj_36
?>

chevron_right


Output :



After Swapping: x = 5, y = 10

Problems with above methods
1) The multiplication and division based approach doesn’t work if one of the numbers is 0 as the product becomes 0 irrespective of the other number.

2) Both Arithmetic solutions may cause arithmetic overflow. If x and y are too large, addition and multiplication may go out of integer range.

3) When we use pointers to variable and make a function swap, all of the above methods fail when both pointers point to the same variable. Let’s take a look what will happen in this case if both are pointing to the same variable.

// Bitwise XOR based method
x = x ^ x; // x becomes 0
x = x ^ x; // x remains 0
x = x ^ x; // x remains 0

// Arithmetic based method
x = x + x; // x becomes 2x
x = x – x; // x becomes 0
x = x – x; // x remains 0

Let us see the following program.

C

#include
void swap(int* xp, int* yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}

int main()
{
int x = 10;
swap(&x, &x);
printf(“After swap(&x, &x): x = %d”, x);
return 0;
}

PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
function swap(&$xp, &$yp)
{
    $xp = $xp ^ $yp;
    $yp = $xp ^ $yp;
    $xp = $xp ^ $yp;
}
  
// Driver Code
$x = 10;
swap($x, $x);
print("After swap(&x, &x): x = " . $x);
  
// This code is contributed
// by chandan_jnu
?>

chevron_right



Output :

After swap(&x, &x): x = 0

Swapping a variable with itself may needed in many standard algorithms. For example see this implementation of QuickSort where we may swap a variable with itself. The above problem can be avoided by putting a condition before the swapping.

C

#include
void swap(int* xp, int* yp)
{
if (xp == yp) // Check if the two addresses are same
return;
*xp = *xp + *yp;
*yp = *xp – *yp;
*xp = *xp – *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf(“After swap(&x, &x): x = %d”, x);
return 0;
}

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of above approach
class GFG {
  
    static void swap(int xp, int yp)
    {
        if (xp == yp) // Check if the two addresses are same
            return;
        xp = xp + yp;
        yp = xp - yp;
        xp = xp - yp;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int x = 10;
        swap(x, x);
        System.out.println("After swap(&x, &x): x = " + x);
    }
}
  
// This code is Contributed by Code_Mech.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of above approach
using System;
class GFG {
  
    static void swap(int xp, int yp)
    {
        if (xp == yp) // Check if the two addresses are same
            return;
        xp = xp + yp;
        yp = xp - yp;
        xp = xp - yp;
    }
  
    // Driver code
    public static void Main()
    {
        int x = 10;
        swap(x, x);
        Console.WriteLine("After swap(&x, &x): x = " + x);
    }
}
  
// This code is Contributed by Code_Mech.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
function swap($xp, $yp)
{
    // Check if the two addresses 
    // are same
    if ($xp == $yp
        return;
    $xp = $xp + $yp;
    $yp = $xp - $yp;
    $xp = $xp - $yp;
}
  
// Driver Code
$x = 10;
swap($x, $x);
echo("After swap(&x, &x): x = " . $x);
return 0;
  
// This code is contributed 
// by Code_Mech.

chevron_right



Output :

After swap(&x, &x): x = 10

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up