Swapping Two Numbers Without Using a Third Variable in C

Swapping two numbers is a common programming task, and it’s an excellent exercise for beginners to understand basic arithmetic operations and variable manipulation. In this blog post, we’ll learn how to swap two numbers without using a third variable. This method is both simple and effective, making it perfect for those who are new to programming.

Understanding the Concept

Normally, when we think about swapping two values, we might consider using a temporary variable to hold one of the values during the swap. However, there’s a neat trick using basic arithmetic operations that allows us to swap the values without needing any extra space.

Steps to Swap Without a Third Variable

Let’s consider two variables a and b. The idea is to use addition and subtraction to rearrange the values:

  1. Step 1: Add b to a and store the result in a.
  2. Step 2: Subtract b (original value) from the new a and store the result in b.
  3. Step 3: Subtract the new b from the new a and store the result in a.

By following these steps, we effectively swap the values of a and b without using a third variable.

Example Program

Here’s a simple C program to demonstrate this technique:

#include <stdio.h>

int main() {
    int a, b;

    // Input two numbers
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);

    // Print initial values
    printf("Before swapping: a = %d, b = %d\n", a, b);

    // Swap the values without using a third variable
    a = a + b;
    b = a - b;
    a = a - b;

    // Print swapped values
    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;
}

Detailed Explanation

Let’s break down the program step by step:

  1. Input Two Numbers: The program starts by asking the user to enter two numbers, which are stored in variables a and b. printf("Enter first number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", &b);
  2. Print Initial Values: The program then prints the values of a and b before swapping. printf("Before swapping: a = %d, b = %d\n", a, b);
  3. Swap the Values: The actual swapping is done using three arithmetic operations. a = a + b; // Now a contains the sum of both numbers b = a - b; // Subtract original b from the sum, now b contains the value of original a a = a - b; // Subtract new b (which is original a) from the sum, now a contains the value of original b
  4. Print Swapped Values: Finally, the program prints the swapped values of a and b. printf("After swapping: a = %d, b = %d\n", a, b);

Testing the Program

When you run the program, it will prompt you to enter two numbers. For example:

Enter first number: 5
Enter second number: 10

The output will show the values before and after swapping:

Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Conclusion

Swapping two numbers without using a third variable is a useful trick for beginners to learn. It helps reinforce the understanding of basic arithmetic operations and variable manipulation. Try experimenting with different values and see how the program works. Happy coding!

Feel free to leave your questions or comments below if you need further assistance or clarification.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top