Site icon DataFlair

C program to Swap Two Numbers

swapping of two numbers in c

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

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:

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:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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.

Exit mobile version