Swapping two numbers is a fundamental concept in programming, often serving as an introductory exercise for beginners. In C programming, there are multiple ways to swap two numbers. This article will delve into three different methods to Swap two numbers in C, each with a detailed explanation.
Learn how to “Swap two numbers in C”
Method 1) Swap two numbers in C using a Temporary Variable
This straightforward method commonly introduces the concept of swapping. It uses a temporary variable to hold one of the numbers during the swap process.
Steps:
- Store the value of the first variable in a temporary variable.
- Assign the value of the second variable to the first variable.
- Assign the value of the temporary variable to the second variable.
Code Example
#include <stdio.h> int main() { int a, b, temp; // Initializing variables printf("Enter two integers: "); scanf("%d %d", &a, &b); // Swapping using a temporary variable temp = a; a = b; b = temp; // Output the result printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
Explanation of the Code
- The program declares three integer variables: a, b, and temp.
- It prompts the user to input two integers, which it stores in a and b.
- It stores the value of a in temp.
- Then, it assigns the value of b to a.
- Finally, it assigns the value stored in temp (originally a) to b.
Method 2) Swap two numbers in C using Arithmetic Operations
This method eliminates the need for a temporary variable by using arithmetic operations. It relies on the properties of addition and subtraction to swap the values.
Steps:
- Add the two numbers and store the result in the first variable.
- Subtract the second variable from the first variable and assign the result to the second variable.
- Subtract the updated second variable from the first variable and assign the result to the first variable.
Code Example
#include <stdio.h> int main() { int a, b; // Initializing variables printf("Enter two integers: "); scanf("%d %d", &a, &b); // Swapping using arithmetic operations a = a + b; b = a - b; a = a - b; // Output the result printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
Explanation of the Code
- The program begins by declaring two integer variables,
a
andb
. - The user inputs two integers, which are stored in
a
andb
. - The values of
a
andb
are added and the result is stored ina
. - The value of
b
is subtracted from the new value ofa
(which is the sum ofa
andb
), and the result is stored inb
. - Finally, the updated value of
b
is subtracted from the new value ofa
(still the sum of the originala
andb
), and the result is stored ina
.
Method 3) Swap two numbers in C using Bitwise XOR Operator in C
This method uses the XOR bitwise operator to swap the numbers without using a temporary variable. It is a bit more advanced but demonstrates the versatility of bitwise operations in C.
Steps:
- Apply the XOR operation between the two numbers and store the result in the first variable.
- Apply the XOR operation between the updated first variable and the second variable, and store the result in the second variable.
- Then, apply the XOR operation between the updated first variable and the updated second variable, and store the result in the first variable.
Code Example
#include <stdio.h> int main() { int a, b; // Initializing variables printf("Enter two integers: "); scanf("%d %d", &a, &b); // Swapping using bitwise XOR operator a = a ^ b; b = a ^ b; a = a ^ b; // Output the result printf("After swapping: a = %d, b = %d\n", a, b); return 0; }
Explanation of the Code
- The program declares two integer variables, a and b.
- The user inputs two integers, which it stores in a and b.
- It performs the XOR operation between a and b and stores the result in a.
- hen, it performs the XOR operation between the updated a and b and stores the result in b.
- Finally, it performs the XOR operation between the updated a and the updated b and stores the result in a.