In C, a Swap Two Numbers program exchanges the values of two variables, meaning that the value of the first variable is assigned to the second, and the value of the second is assigned to the first. This can be achieved either by using a temporary variable or without a temporary variable using arithmetic or bitwise operations.
Methods to Swap Two Numbers:
- Using a Temporary Variable.
- Without a Temporary Variable using arithmetic operations.
- Without a Temporary Variable using bitwise XOR operator.
Method 1: Swapping Two Numbers Using a Temporary Variable
This is the most straightforward method where we use a third variable to temporarily hold the value of one of the numbers during the swap.
Program:
Explanation:
Variables:
int a, b, temp
:a
andb
are the two numbers to be swapped, andtemp
is the temporary variable used to store one value during the swap.
Input:
scanf("%d %d", &a, &b);
takes two integer inputs from the user and stores them ina
andb
.
Swapping Logic:
temp = a;
stores the value ofa
in the temporary variabletemp
.a = b;
assigns the value ofb
toa
.b = temp;
assigns the value stored intemp
(the original value ofa
) tob
.
Output:
- The values of
a
andb
are printed both before and after the swap to demonstrate the swapping.
- The values of
Sample Output:
Example:
Method 2: Swapping Two Numbers Without a Temporary Variable (Using Arithmetic Operations)
We can also swap two numbers using arithmetic operations like addition and subtraction.
Program:
Explanation:
Swapping Logic:
a = a + b;
: This addsa
andb
and stores the result ina
.b = a - b;
: The new value ofa
is subtracted fromb
, which gives the original value ofa
, and this is assigned tob
.a = a - b;
: The new value ofb
(which is the original value ofa
) is subtracted from the new value ofa
, which gives the original value ofb
, and this is assigned toa
.
Output:
- The program prints the values of
a
andb
both before and after the swap, demonstrating the arithmetic-based swapping.
- The program prints the values of
Sample Output:
Example:
Method 3: Swapping Two Numbers Without a Temporary Variable (Using XOR Bitwise Operator)
Another way to swap numbers without a temporary variable is by using the XOR (^
) operator.
Program:
Explanation:
Swapping Logic:
a = a ^ b;
: XORa
andb
, and store the result ina
.b = a ^ b;
: XOR the new value ofa
(which containsa ^ b
) withb
. This results in the original value ofa
, which is then assigned tob
.a = a ^ b;
: XOR the new value ofa
(which containsa ^ b
) with the new value ofb
(which now contains the original value ofa
). This gives the original value ofb
, which is assigned toa
.
Output:
- The program prints the values of
a
andb
both before and after the swap, demonstrating the XOR-based swapping.
- The program prints the values of
Sample Output:
Example:
Key Points:
- Using a Temporary Variable: The most intuitive and common method.
- Without a Temporary Variable (Arithmetic): Uses addition and subtraction but can lead to overflow for large values.
- Without a Temporary Variable (XOR): Works using bitwise operations but might be harder to understand for beginners.
Each of these methods swaps two numbers, demonstrating different ways to perform the same task in C.