Swap Two Numbers Without Using a Third Variable – Simple Program

Swap Two Numbers Without Using a Third Variable – Simple Program

Swap Two Numbers Without Using a Third Variable – Simple Program

Swapping two numbers is a common programming problem often asked in coding interviews and exams. While the traditional approach uses a temporary variable, there are multiple ways to swap two numbers without using an extra variable. In this article, we will explore different methods to achieve this efficiently.

Why Avoid a Third Variable?

Using a third variable for swapping is straightforward but not always efficient. Avoiding an extra variable helps in optimizing memory usage and demonstrates a better understanding of algorithmic logic. This is particularly useful in competitive programming and low-memory environments.

Method 1: Using Arithmetic Operations

One of the simplest ways to swap two numbers without a temporary variable is by using addition and subtraction.

Code Implementation:

#include <stdio.h>

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

Explanation:

  1. a = a + b → a becomes 15 (5+10)
  2. b = a - b → b becomes 5 (15-10)
  3. a = a - b → a becomes 10 (15-5)

Best Use Case:

  • Works well when overflow is not a concern.

Suggested Visual:

  • A flowchart illustrating the swapping steps.

Method 2: Using Bitwise XOR

Bitwise XOR is a highly efficient way to swap numbers without using extra memory.

Code Implementation:

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swapping: a = %d, b = %d\n", a, b);
    
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}

Explanation:

  1. a = a ^ b → stores bitwise XOR of a and b in a.
  2. b = a ^ b → restores original value of a into b.
  3. a = a ^ b → restores original value of b into a.

Best Use Case:

  • Works efficiently in low-memory environments.

Suggested Visual:

  • A bitwise operation diagram showing XOR logic.

Method 3: Using Multiplication and Division

Another alternative is using multiplication and division.

Code Implementation:

#include <stdio.h>

int main() {
    int a = 5, b = 10;
    printf("Before swapping: a = %d, b = %d\n", a, b);
    
    a = a * b;
    b = a / b;
    a = a / b;
    
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}

Explanation:

  1. a = a * b → stores product of a and b in a.
  2. b = a / b → restores original value of a into b.
  3. a = a / b → restores original value of b into a.

Best Use Case:

  • Works well when dealing with large integers but not recommended for floating-point numbers due to precision issues.

Suggested Visual:

  • A step-by-step breakdown of multiplication and division swaps.

Key Takeaways

  • Arithmetic method is simple but can cause overflow.
  • Bitwise XOR is efficient and avoids overflow.
  • Multiplication and division work but are risky with floating-point numbers.

Conclusion

Swapping two numbers without using a third variable is an essential concept in programming. Understanding multiple methods helps in writing optimized code suitable for different scenarios. If you’re preparing for coding interviews, practicing these techniques will enhance your problem-solving skills.

Swap Two Numbers Without Using a Third Variable – Simple Program