0
0
CProgramBeginner · 2 min read

C Program to Swap Two Numbers Without Temp Variable

You can swap two numbers without a temp variable in C using arithmetic: a = a + b; b = a - b; a = a - b; or bitwise XOR: a ^= b; b ^= a; a ^= b;.
📋

Examples

Inputa=5, b=10
OutputAfter swap: a=10, b=5
Inputa=0, b=0
OutputAfter swap: a=0, b=0
Inputa=-3, b=7
OutputAfter swap: a=7, b=-3
🧠

How to Think About It

To swap two numbers without a temporary variable, use math or bitwise operations that combine and separate values. For example, adding both numbers stores their sum in one variable, then subtracting the other number recovers the original values in swapped order.
📐

Algorithm

1
Take two input numbers a and b.
2
Add b to a and store in a.
3
Subtract b from the new a and store in b.
4
Subtract the new b from a and store in a.
5
Now a and b are swapped without using extra space.
💻

Code

c
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swap: a=%d, b=%d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
Output
Before swap: a=5, b=10 After swap: a=10, b=5
🔍

Dry Run

Let's trace swapping a=5 and b=10 through the code.

1

Initial values

a=5, b=10

2

Add b to a

a = 5 + 10 = 15

3

Subtract b from new a to get new b

b = 15 - 10 = 5

4

Subtract new b from new a to get new a

a = 15 - 5 = 10

5

Swapped values

a=10, b=5

Stepab
Initial510
a = a + b1510
b = a - b155
a = a - b105
💡

Why This Works

Step 1: Sum stores combined value

Adding a and b stores their total in a, keeping both values combined.

Step 2: Recover original a in b

Subtracting original b from the sum gives original a, now stored in b.

Step 3: Recover original b in a

Subtracting new b (original a) from sum gives original b, now stored in a.

🔄

Alternative Approaches

Bitwise XOR swap
c
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swap: a=%d, b=%d\n", a, b);
    a ^= b;
    b ^= a;
    a ^= b;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
Uses bitwise XOR to swap without arithmetic; avoids overflow but only works with integers.
Using multiplication and division
c
#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swap: a=%d, b=%d\n", a, b);
    a = a * b;
    b = a / b;
    a = a / b;
    printf("After swap: a=%d, b=%d\n", a, b);
    return 0;
}
Swaps using multiplication and division but can cause errors if either number is zero.

Complexity: O(1) time, O(1) space

Time Complexity

Swapping uses a fixed number of operations, so it runs in constant time O(1).

Space Complexity

No extra variables are used, so space complexity is O(1), swapping in place.

Which Approach is Fastest?

All methods run in O(1) time; bitwise XOR avoids overflow but is less intuitive than arithmetic.

ApproachTimeSpaceBest For
Arithmetic swapO(1)O(1)Simple integers, watch overflow
Bitwise XOR swapO(1)O(1)Integers, no overflow risk
Multiplication/division swapO(1)O(1)Avoid zero values, less safe
💡
Use arithmetic or bitwise XOR to swap without extra memory, but watch out for overflow or zero values.
⚠️
Forgetting that arithmetic swap can overflow or fail if numbers are too large or zero.