C program to Swap Two Numbers

Get Certified in C Programming for Free and Take Your Skills to the Next Level

Swapping two numbers is a common task in programming, often used in various algorithms and applications. It involves exchanging the values of two variables, allowing the values to be reassigned to different variables. This article will provide a comprehensive guide to swapping two numbers using C program language, including explanations and code snippets.

Approaches for Swapping Two Numbers in C:

There are multiple approaches to swapping two numbers in C. The most common methods involve using a temporary variable, bitwise XOR operation, or arithmetic operations. Let’s explore each method in detail.

1. Swap two numbers in C Using a Temporary Variable:

One straightforward method to swap two numbers is by using a temporary variable. The following is the algorithm used in this method

  • Set the initial number’s value to a temporary variable.
  • Set the next number’s value to the original value.
  • Set the temporary variable’s contents to the second number.

Here’s the code snippet illustrating the swapping using a temporary variable:

#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int DataFlair = 45;
int Other = 50;
}
printf("Before swapping: DataFlair = %d, Other = %d\n”, DataFlair, Other);
swap (&DataFlair, &Other);
printf("After swapping: DataFlair = %d, flair = %d\n”, DataFlair, Other););
return 0;

Output:

Before swapping: DataFlair = 45, Other = 49
After swapping: Other = 45, DataFlair = 49

2. Swap two numbers in C using Bitwise XOR Operation:

Another efficient approach to swap two numbers in C is by using the bitwise XOR (^) operation. This method does not require a temporary variable and works by manipulating the bits of the numbers. The algorithm involved in this approach is as follows:

  • Perform the bitwise XOR operation between the first and second numbers and store the result in one of the variables.
  • Perform the bitwise XOR operation between the result obtained above and the first number, storing the new result in the second variable.
  • Finally, Perform the bitwise XOR operation between the result obtained above and the second number, storing the new result in the first variable.

Here’s the code snippet illustrating the swapping using the bitwise XOR operation:

#include <stdio.h>
void swap(int* a, int* b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main() {
int DataFlair = 15;
int Other = 72;
printf("Before swapping: DataFlair = %d, Other = %d\n", DataFlair, Other);
swap (&DataFlair, &Other);
printf("After swapping: DataFlair = %d, Other = %d\n", DataFlair, Other);
return 0;
}

Output:

Before swapping: DataFlair = 15, Other = 72
After swapping: Other = 72, DataFlair = 15

3. C program to Swap Two Numbers using Arithmetic Operations:

Swapping two numbers can also be achieved using arithmetic operations. This method relies on addition and subtraction to reassign values. The algorithm involved in this approach is given:

  • Add the first and second numbers and store the sum in the first variable.
  • Subtract the second number from the sum obtained above and store the difference in the second variable.
  • Subtract the difference obtained above from the sum and store the result in the first variable.

Here’s the code snippet illustrating the swapping using arithmetic operations:

#include <stdio.h>
void swap(int* a, int* b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main() {
int DataFlair  = 20;
int Other = 24;
printf("Before swapping: DataFlair =%d, Other = %d\n", DataFlair, Other);
swap (&DataFlair, &flair);
printf("After swapping: DataFlair =%d, Other =%d\n", DataFlair, Other);
return 0;
}

Output:

Before swapping: DataFlair = 20, Other = 24
After swapping: Other = 24, DataFlair = 20

Conclusion

Swapping two numbers in C is a fundamental operation that finds applications in various programming scenarios. Whether using a temporary variable, bitwise XOR operation, or arithmetic operations, understanding the different approaches enables programmers to efficiently exchange values between variables. By implementing the provided code snippets, developers can easily incorporate the swapping functionality into their C programs, facilitating tasks such as sorting algorithms, data reordering, or value shuffling.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *