# Swapping of Two Numbers in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Topics Covered

Swapping of two numbers in C is a fundamental programming concept, often used to reorder elements in data structures like arrays and lists. This simple yet powerful technique is foundational in various sorting algorithms and other operations that require element repositioning.

## Swapping Two Numbers Using a Temporary Variable

Swapping of two numbers in c using a temporary variable is one of the most straightforward methods in C programming. This technique employs a third variable to temporarily hold the value of one of the numbers during the exchange process, ensuring that no data is lost.

Example

Output

Complexity Analysis

• Time Complexity: The time complexity of this swapping method is O(1), or constant time complexity. This means that the operation takes a fixed amount of time to execute, regardless of the input values.

• Space Complexity: The space complexity is also O(1), as the method uses a single temporary variable temp for swapping, which occupies a constant amount of memory space.

## Swapping of Two Numbers in C Without using a Temporary Variable in C

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 C program to swap two numbers:

Output:

### 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 C program to swap two numbers:

Output:

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 C program to swap two numbers:

Output:

## Conclusion

1. Swapping program in c is a basic yet crucial operation, essential for algorithms that involve reordering elements, such as sorting algorithms.

2. Using a temporary variable for swapping is the most straightforward and intuitive method.

3. This method boasts a constant time complexity O(1), making it highly efficient for swapping operations.