Search for Courses, Topics

# Swapping of Two Numbers in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

## Overview

When you hear the word “Swap”, what comes to your mind?

Interchanging things from one position to another, right? In the same manner here, it refers to mutually exchanging the values of the variables.

In Swapping two numbers, we simply interchange the values of the two variables.

## Scope of the Article

• In this article, we will understand how to swap two variables using a third variable.
• We will also see various methods to swap 2 numbers without using a third variable.

## Swapping Using Third Variable

This method is also popularly known as Swapping Using Temporary Variable.

Let’s take an interesting example to understand this concept well.

Suppose we have three glasses. Glass A is filled with red liquid, glass B is filled with green liquid, while glass C is empty. We need to interchange the liquids in glass A & B with the help of glass C.

Simply pour the red liquid from glass A to C. Then pour the green liquid from glass B to A. Finally, pour the liquid from glass C to B.

Bingo, we have interchanged the liquids. Here glass C acted as the third variable.

Let’s understand the logic behind this by taking an example. Here we will be using the function method. We will use the call by value method.

We will ask users to input two values for the variables a & b, respectively. Then we pass those values through the swapping(a, b) function.

The values of both a and b are entered into the local variables of the swapping(a, b) function, that is x and y, respectively.

The Following logic was followed inside the swapping(a, b) function:

1. Here, we have used “third” as the temporary variable, in which the value of the first variable(x) was assigned.
2. Then, the value of the second variable(y) was assigned to the first variable(x).
3. Finally, the third variable(which holds the value of x(first variable)) is assigned to the second variable(y).

We have successfully swapped the values of x and y.

The code for a function to Swap two numbers with a temporary variable is as follows:

#include <stdio.h>

void swapping(int, int);  // Function declaration.

int main() {
int a, b;

printf("Enter values for a and b respectively: \n");
scanf("%d %d", &a, &b);

printf("The values of a and b BEFORE swapping are a = %d & b = %d \n", a, b);

swapping(a, b);      // Function call.
return 0;
}

void swapping(int x, int y) {   // Function definition.

int third;
third = x;
x = y;
y = third;

printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
}


Output:

Enter values for a and b, respectively:
Input = 10, 20
The values of a and b BEFORE swapping are a = 10 & b = 20
The values of a and b AFTER swapping are a = 20 & b = 10


## Swapping Without Using Third Variable

Here we will discuss some methods in which we won’t be using a temporary variable to Swap numbers. Each of these is explained with some logic and code snippets.

The most common three methods are as follows:

### 1. Swapping Using Addition and Subtraction(+ & -)

Here we won’t be using any temporary variable, instead will swap two numbers simply by making use of addition and subtraction concepts. We will understand this through an example. Let’s take the value of x = 40 & y =10.

• Store the sum of the values of both variables(x & y) in the 1st variable(x).

$x = x + y$

$x = 40 + 10$

Hence, x = 50 and y = 10.

• Store the result in the 2nd variable(y), after subtracting the value of y from the value of x.

KaTeX parse error: Expected 'EOF', got '–' at position 7: y = x –̲ y

KaTeX parse error: Expected 'EOF', got '–' at position 8: y = 50 –̲ 10

Hence, y = 40 (this was the value of x earlier)

• Finally, store the result in x, after subtracting the value of y from the value of x.

KaTeX parse error: Expected 'EOF', got '–' at position 7: x = x –̲ y

KaTeX parse error: Expected 'EOF', got '–' at position 8: x = 50 –̲ 40

Hence, x = 10 (this was value of y earlier).

Following is the functional code for swapping(x, y) function:

void swapping(int x, int y) {
x = x + y;    //1
y = x - y;    //2
x = x - y;    //3

printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
}


Output:

Enter values for a and b, respectively:
Input = 20, 50
The values of a and b BEFORE swapping are a = 20 & b = 50
The values of a and b AFTER swapping are a = 50 & b = 20


Note: We can also execute this logic in another way, which makes the code quite smaller.

We just need to replace statements 1,2,3 in the above code with the following line.

x = x + y - (y = x);


Here if we look closely at the above line. We have assigned x to y first, in the brackets. Then we have subtracted the value of x(after assigning it to y) from the total(x + y) to assign the value of y(which is left after subtracting) to x.

### 2. Swapping Using Multiplication and Division(* & /)

We can also make use of the multiplication and division concepts to swap two numbers, just like we did in addition and subtraction. We will understand the steps to solve this through an example. Let’s take the value of x = 10 & y = 2 for now.

• Store the result in the 1st variable(x) after multiplying the values of both variables(x & y).

$x = x * y$

$x = 10 * 2$

Hence, x = 20.

• Store the result in the 2nd variable(y), after dividing the value of y from the value of x.

$y = x/y$

$y = 20/2$

Hence, y = 10 (this was the value of x earlier).

• Finally, store the result in x, after dividing the value of y from the value of x.

$x = x/y$

$x = 20/10$

Hence, x = 2 (this was value of y earlier).

Following is the functional code for swapping(x, y) function:

void swapping(int x, int y) {
x = x * y;   //1
y = x / y;   //2
x = x / y;   //3

printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
}


Output:

Enter values for a and b respectively:
Input = 40, 60
The values of a and b BEFORE swapping are a = 40 & b = 60
The values of a and b AFTER swapping are a = 60 & b = 40


Note: This method also works well for irrational division(i.e. if we take 3 & 5, 10 & 3, etc for swapping), as after dividing it only takes the integer part.

### 3. Swapping Using Bitwise XOR

We can also use the bitwise XOR operator to swap two numbers, which is not quite a common method.

Note: This method doesn’t work for floating numbers, pointers.

Following is the functional code for swapping(x, y) function:

void swapping(int x, int y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;

printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
}


Output:

Enter values for a and b, respectively:
Input = 70, 90
The values of a and b BEFORE swapping are a = 70 & b = 90
The values of a and b AFTER swapping are a = 90 & b = 70


## Conclusion

So in this article, we learnt:

• How to swap two variables using a third variable.
• Swapping using addition and subtraction
• Swapping using multiplication and division.
• Swapping using bitwise XOR.
Challenge Time!
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
Free Courses by top Scaler instructors