C Program to Swap Two Numbers with Output and Explanation
You can swap two numbers in C by using a temporary variable like this:
temp = a; a = b; b = temp; which exchanges the values of a and b.Examples
Inputa = 5, b = 10
OutputAfter swapping: a = 10, b = 5
Inputa = -3, b = 7
OutputAfter swapping: a = 7, b = -3
Inputa = 0, b = 0
OutputAfter swapping: a = 0, b = 0
How to Think About It
To swap two numbers, think of holding one number temporarily so you don't lose it when you overwrite it. Use a temporary container to store one number, then replace it with the other, and finally put the stored number into the second variable.
Algorithm
1
Get input values for two numbers a and b2
Store the value of a in a temporary variable temp3
Assign the value of b to a4
Assign the value of temp to b5
Print the swapped values of a and bCode
c
#include <stdio.h> int main() { int a, b, temp; printf("Enter two numbers: "); scanf("%d %d", &a, &b); temp = a; a = b; b = temp; printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
Output
Enter two numbers: 5 10
After swapping: a = 10, b = 5
Dry Run
Let's trace swapping a=5 and b=10 through the code
1
Input values
a = 5, b = 10
2
Store a in temp
temp = 5
3
Assign b to a
a = 10
4
Assign temp to b
b = 5
5
Print swapped values
a = 10, b = 5
| Variable | Value |
|---|---|
| a | 5 -> 10 |
| b | 10 -> 5 |
| temp | 5 |
Why This Works
Step 1: Use a temporary variable
We use temp to hold the value of a so it is not lost when a is overwritten.
Step 2: Swap values
Assign b to a, then assign the stored temp value to b to complete the swap.
Step 3: Print result
Display the new values of a and b to confirm the swap.
Alternative Approaches
Swap without temporary variable using arithmetic
c
#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); a = a + b; b = a - b; a = a - b; printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
This method avoids extra memory but can cause overflow if numbers are large.
Swap using bitwise XOR operator
c
#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); a = a ^ b; b = a ^ b; a = a ^ b; printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
This method uses bitwise operations and no extra variable but is less readable.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers uses a fixed number of operations, so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All methods run in O(1) time; using a temporary variable is simplest and safest, while arithmetic or XOR methods save memory but risk overflow or reduce readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary variable | O(1) | O(1) | Clarity and safety |
| Arithmetic operations | O(1) | O(1) | Memory saving but risk overflow |
| Bitwise XOR | O(1) | O(1) | Memory saving, less readable |
Always use a temporary variable to swap values safely and clearly.
Forgetting to use a temporary variable causes one value to be overwritten and lost.