How to swap two numbers without using a temporary variable?

In this blog post, I will teach you how to write a C/C++ program to swap two numbers without using a temporary variable. After reading this detailed guide blog post, I believe you can write an efficient program to swap two numbers without using a temporary variable.

I have already written a blog post “C Program to swap two numbers”, where I have different ways to swap two numbers. But here you will learn how to swap two numbers without using a temporary variable.

Let’s see the problem statements for this question.

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

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

Approach-1 Swap Variables Using the XOR Bitwise Operator:

The bitwise XOR operator can be used to swap two integer numbers. This approach is a clever method to swap values without a temporary variable.

The result of the XOR operator is the bitwise exclusive OR of the operands x and y. That means the XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ.

☠️Note: This approach will not work when x and y both are the same integer number.

C Program to swap two numbers using the XOR Bitwise Operator:

/*
C program to swap two numbers
without using a temporary variable?.
*/
#include<stdio.h>

void swapTwoNumber(int *x, int *y)
{
    /*return without swapping
    if x and y both are the same
    */
    if(*x == *y)
        return;
    *x = *x ^ *y;
    *y = *x ^ *y;
    *x = *x ^ *y;
}
int main()
{
    int x = 10;
    int y = 20;

    //before swapping x and y
    printf("x = %d and y = %d\n\n",x,y);

    swapTwoNumber(&x, &y);

    //after swapping x and y
    printf("x = %d and y = %d\n",x,y);

    return 0;
}

Output:

x = 10 and y = 20

x = 20 and y = 10

 

C++ Program to swap two numbers using the XOR Bitwise Operator:

/*
C++ program to swap two numbers
without using a temporary variable?.
*/
#include<iostream>
using std::cout;
using std::endl;


void swapTwoNumber(int *x, int *y)
{
    /*return without swapping
    if x and y both are the same
    */
    if(*x == *y)
        return;
    *x = *x ^ *y;
    *y = *x ^ *y;
    *x = *x ^ *y;
}
int main()
{
    int x = 10;
    int y = 20;

    //before swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    swapTwoNumber(&x, &y);

    //after swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    return 0;
}

Output:

x = 10 and y = 20
x = 20 and y = 10

 

Here’s how the above method works step by step:

Let’s assume the initial value of ‘x’  is set to 10, and ‘y’ is set to 20. In binary, x is 00001010, and y is 00010100.

Step-1: XORing ‘x’ and ‘y’:

The XOR operator (^) compares the binary representations of x  and y  bit by bit. Each bit in the result is set if and only if exactly one of the corresponding bits in the ‘x’ or ‘y’ is set.

So the result is 00011110 in binary, which is 30 in decimal. Therefore, after the first step, x becomes 30.

Step-2: XORing new ‘x’ and ‘y’:

Here XORing between the y (00010100) and new x ( 00011110 ).

Now the result would be 10 (00001010) and it is assigned back to ‘y’.

Step-3: XORing new ‘x’ and new ‘y’:

Here XORing between the new y (00001010) and new x ( 00011110 ).

Now the result would be 20 (00010100) and it is assigned back to ‘x’.

After the third step, the values of x and y have been effectively swapped. And now with the help of the XOR operation, you can swap two numbers without using an additional temporary variable.

 

Approach-2 Using Addition and subtraction:

Addition and subtraction approach is also an easy way to swap two numbers without using a temporary variable.

In this approach, idea is to first get the sum of two given number and store the result in one of them. In the later steps subtract the numbers from the sum to swap the numbers. Let’s see the example for the better understanding.

☠️Note: This approach does not work when the sum of x and y (x+y) is overflow.

C Program to swap two numbers using addition and subtraction:

/*
C program to swap two numbers
without using a temporary variable?.
*/

#include<stdio.h>
void swapTwoNumber(int *x, int *y)
{
    /*return without swapping
    if two addresses are same
    */
    if(x == y)
        return;
    *x = *x + *y;
    *y = *x - *y;
    *x = *x - *y;
}

int main()
{
    int x = 10;
    int y = 20;

    //before swapping x and y
    printf("x = %d and y = %d\n\n",x,y);

    swapTwoNumber(&x, &y);

    //after swapping x and y
    printf("x = %d and y = %d\n",x,y);

    return 0;
}

Output:

x = 10 and y = 20
x = 20 and y = 10

 

C++ Program to swap two numbers using addition and subtraction:

/*
C++ program to swap two numbers
without using a temporary variable?.
*/
#include<iostream>
using std::cout;
using std::endl;


void swapTwoNumber(int *x, int *y)
{
    /*return without swapping
    if two addresses are same
    */
    if(x == y)
        return;
    *x = *x + *y;
    *y = *x - *y;
    *x = *x - *y;
}


int main()
{
    int x = 5;
    int y = 20;

    //before swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    swapTwoNumber(&x, &y);

    //after swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    return 0;
}

Output:

x = 5 and y = 20
x = 20 and y = 5

 

Approach-3 Using Multiplication and division:

You can also swap the two number using the Multiplication and division.

☠️ Note: This approach does not work when the multiplication of x and y (x*y) is overflow.

 

C Program to swap two numbers using Multiplication and division:

/*
C program to swap two numbers
without using a temporary variable?.
*/

#include<stdio.h>

void swapTwoNumber(int *x, int *y)
{
    if (*y == 0)
    {
        *y = *x;
        *x = 0;
    }
    else if (*x == 0)
    {
        *x = *y;
        *y = 0;
    }
    // Code to swap 'x' and 'y'
    else
    {
        *x = ((*x) * (*y)); // x now becomes 100
        *y = (*x) / (*y); // y becomes 5
        *x = (*x) / (*y); // x becomes 20
    }

}

int main()
{
    int x = 5;
    int y = 20;

    //before swapping x and y
    printf("x = %d and y = %d\n\n",x,y);

    swapTwoNumber(&x, &y);

    //after swapping x and y
    printf("x = %d and y = %d\n",x,y);

    return 0;
}

Output:

x = 5 and y = 20
x = 20 and y = 5

 

C++ Program to swap two numbers using Multiplication and division:

/*
C++ program to swap two numbers
without using a temporary variable?.
*/
#include<iostream>
using std::cout;
using std::endl;


void swapTwoNumber(int *x, int *y)
{
    if (*y == 0)
    {
        *y = *x;
        *x = 0;
    }
    else if (*x == 0)
    {
        *x = *y;
        *y = 0;
    }
    // Code to swap 'x' and 'y'
    else
    {
        *x = ((*x) * (*y)); // x now becomes 100
        *y = (*x) / (*y); // y becomes 5
        *x = (*x) / (*y); // x becomes 20
    }

}


int main()
{
    int x = 5;
    int y = 20;

    //before swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    swapTwoNumber(&x, &y);

    //after swapping x and y
    cout<<"x = "<<x<<" and y = "<<y<<endl;

    return 0;
}

Output:

x = 5 and y = 20
x = 20 and y = 5

 

If you want, you can do swapping two numbers in one line using the below expression, but it is not recommended to do.

//Using ex-or
x = x ^ y ^ (y = x);


//Using addition subtraction
x = x + y – (y = x);


//Using multiplication division
x = (x * y) / (y = x);

 

Recommended Post