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:
- Step 1: Add
b
toa
and store the result ina
. - Step 2: Subtract
b
(original value) from the newa
and store the result inb
. - Step 3: Subtract the new
b
from the newa
and store the result ina
.
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:
- Input Two Numbers: The program starts by asking the user to enter two numbers, which are stored in variables
a
andb
.printf("Enter first number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", &b);
- Print Initial Values: The program then prints the values of
a
andb
before swapping.printf("Before swapping: a = %d, b = %d\n", a, b);
- 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
- Print Swapped Values: Finally, the program prints the swapped values of
a
andb
.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.