Swap Two Numbers Without Using a Temporary Variable (C)

Опубликовано: 28 Май 2026
на канале: pkotsorgios_code
8
1

In this video, we solve a classic C programming problem: swapping two integers without using any extra variable.
We use the addition–subtraction method to perform the swap in constant time.

Input: Two integers a and b
Output: Swapped values of a and b
Method Used: Addition & Subtraction (Dont use it, it is not recomended. Try it just for educational pyrposes).

Language: C
IDE Used: Dev-C++ (works in Code::Blocks, GCC, etc.)
━━━━━━━━━━━━━━━━━━━━━━━
Source Code:

#include stdio.h

int main(){

int a,b;

scanf("%d %d", &a, &b);

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", a,b);

return 0;
}
━━━━━━━━━━━━━━━━━━━━━━━

Playlist: C Problem Solving (Easy to Hard)

👍 If this helped you, like the video
🔔 Subscribe for more C problems, algorithms, and data structures

#cprogramming #coding #problemsolving #algorithms #ccoding #programming