![]() |
VOOZH | about |
Swapping two numbers means exchanging their values. In this article, we will learn how to swap values of two numbers in a C program.
The easiest method to swap two numbers is to use a temporary variable. First, we assign the value of first variable to temporary variable, then assign the value of second variable to first variable and in the last assign the value of temporary variable to second variable, which is the value of first variable.
a = 10, b = 5
In this method, we use arithmetic operations to swap the values without using a temporary variable.
a = 10, b = 5
We can also use the bitwise XOR operator to swap the values without using a temporary variable.
a = 10, b = 5