Swapping two numbers is a fundamental concept in programming that involves exchanging the values of two variables. This article provides an in-depth look at various C Program for Swapping Two Numbers, including real examples with different solutions and outputs. We’ll cover basic swapping with a temporary variable, swapping without a temporary variable, and swapping using bitwise XOR. Each example will be explained in detail, with structured headings and outputs for clarity.
Prerequisites
Before diving into the examples, you should be familiar with:
- Basic syntax and structure of C programs.
- Data types and variable declarations in C.
- Basic input and output functions in C (such as
printf
andscanf
). - Operators in C, including arithmetic and bitwise operators.
1. Swapping Using a Temporary Variable
1.1 Explanation
The most common and straightforward method to swap two numbers is by using a temporary variable. This method is easy to understand and implement.
1.2 Program: Swapping Using a Temporary Variable
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
1.3 Output
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
2. Swapping Without Using a Temporary Variable
2.1 Explanation
Swapping two numbers without using a temporary variable can be done using arithmetic operations. This method utilizes the properties of addition and subtraction.
2.2 Program: Swapping Without Using a Temporary Variable
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
2.3 Output
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
3. Swapping Using Bitwise XOR
3.1 Explanation
The XOR bitwise operation can also be used to swap two numbers without a temporary variable. This method is less common but is a neat trick often asked in technical interviews.
3.2 Program: Swapping Using Bitwise XOR
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
3.3 Output
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
4. Swapping Using Pointers
4.1 Explanation
Swapping using pointers involves passing the addresses of the variables to a function and swapping their values using dereferencing.
4.2 Program: Swapping Using Pointers
#include <stdio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
4.3 Output
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
5. Swapping Using Macros
5.1 Explanation
Using macros in C, we can define a swap operation that can be reused throughout the code.
5.2 Program: Swapping Using Macros
#include <stdio.h>
#define SWAP(a, b) { int temp = a; a = b; b = temp; }
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
SWAP(a, b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
5.3 Output
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
Conclusion
This article covered various C Program for Swapping Two Numbers, including using a temporary variable, without a temporary variable, using bitwise XOR, using pointers, and using macros. Each method has its unique way of achieving the swap, providing multiple approaches depending on the scenario and requirements. By understanding these methods, you can choose the most appropriate one for your specific needs in C programming.