Well we can swap the values of 2 variables in C by using a temporary variable.
The idea is simple,
- Assign a to any temporary variable : temp = a
- Assign b to a : a = b
- Assign temp to b : b = temp
You can also ask the user to input the value sfor the variables, but for the shake of simplicity i commented it out.
Input : x = 10, y = 20 Output : x = 20, y = 10
That’s it, Thanks for reading.