C program to swap two numbers without using a third variable
In this article, we will discuss how to swap two numbers without using a third variable in C.
Swapping two numbers means exchanging the values of two variables. You can swap two numbers without using a third variable using arithmetic operations like addition and subtraction or the bitwise XOR operator.
By using Arithmetic Operations Algorithm:
Mathematical operations, notably addition, and subtraction, can swap two numbers without using a third variable. The goal is to change the values of the two variables by manipulation. The algorithm used to swap two numbers using arithmetic operations is as follows:
- Let num1 and num2 be the two numbers.
- Store the result of adding num1 and num2 in num1.
- Subtract the original value of num2 (before adding) from the new value of num1, and store the result in num2.
- Subtract the original value of num2 (before adding) from the new value of num1, and store the result in num1.
- The values of num1 and num2 will be switched after these procedures.
![C program to swap two numbers without using a third variable](https://static.tutorialandexample.com/c/c-program-to-swap-two-numbers-without-using-a-third-variable1.png)
![C program to swap two numbers without using a third variable](https://static.tutorialandexample.com/c/c-program-to-swap-two-numbers-without-using-a-third-variable2.png)
Let us see an example:
Code:
#include <stdio.h>
//main function
int main() {
int num1, num2;
//inputting the number1,number2
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
//output before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
//algorithm
num1 = num1 + num2; // Step 2: num1 contains the sum of original num1 and num2
num2 = num1 - num2; // Step 3: num2 contains the original value of num1
num1 = num1 - num2; // Step 4: num1 contains the original value of num2
//output after swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Output:
![C program to swap two numbers without using a third variable](https://static.tutorialandexample.com/c/c-program-to-swap-two-numbers-without-using-a-third-variable3.png)
By using the Bitwise Operations Algorithm:
Swapping two numbers without a third variable is possible by utilizing the bitwise XOR (^) method. The XOR operation has the characteristic of returning 1 when the bits are different and 0 when the bits are equal. We can effectively swap the values of two variables by using XOR techniques. The following procedure uses the bitwise XOR operation to swap two numbers:
- Let num1 and num2 be the two numbers.
- Put num1 and num2 through an XOR operation. After that, save the outcome in num1.
- Perform the XOR operation between the new value of num1 (after XOR) and the original value of num2, and store the result in num2.
- Execute an XOR operation between the updated values of num1 and num2 (after XOR), and then store the outcome in num1.
- After these operations, the values of num1 and num2 will be swapped.
Let us see an example:
Code:
#include <stdio.h>
//main function
int main() {
int num1, num2;
//inputting two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
//before swapping output
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
//applying algorithm
num1 = num1 ^ num2; // Step 2: num1 contains the XOR of original num1 and num2
num2 = num1 ^ num2; // Step 3: num2 contains the original value of num1
num1 = num1 ^ num2; // Step 4: num1 contains the original value of num2
//after swapping the output
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Output:
![C program to swap two numbers without using a third variable](https://static.tutorialandexample.com/c/c-program-to-swap-two-numbers-without-using-a-third-variable4.png)
Note:
This algorithm guarantees that the values of num1 and num2 are switched without needing a third variable. However, remember that this approach is only suitable for integer values and that precision loss or overflow concerns may cause problems with floating-point numbers or huge integers. It is preferable to utilize a temporary variable for swapping in these circumstances.