Introduction
Swapping two numbers without using a temporary variable is a common programming exercise that involves arithmetic operations. This guide will show you how to write a C program to swap the values of two variables without using an additional temporary variable.
Problem Statement
Create a C program that:
- Takes two numbers as input from the user.
- Swaps the values of the two numbers without using a temporary variable.
- Displays the numbers after swapping.
Example:
- Input:
a = 5,b = 10 - Output:
a = 10,b = 5
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the two numbers.
- Input the Numbers: Use
scanfto take input from the user for the two numbers. - Swap the Numbers Using Arithmetic Operations: Use addition and subtraction (or XOR operation) to swap the values of the two numbers without a temporary variable.
- Display the Swapped Values: Use
printfto display the numbers after swapping.
C Program (Using Addition and Subtraction)
#include <stdio.h>
/**
* C Program to Swap Two Numbers Without Using a Temporary Variable
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the numbers
int a, b;
// Step 2: Prompt the user to enter the first number
printf("Enter the first number (a): ");
scanf("%d", &a);
// Step 3: Prompt the user to enter the second number
printf("Enter the second number (b): ");
scanf("%d", &b);
// Step 4: Swap the numbers using addition and subtraction
a = a + b; // Step 4.1: Add b to a and store the result in a
b = a - b; // Step 4.2: Subtract b from the new a (which is a+b) to get the original a and assign it to b
a = a - b; // Step 4.3: Subtract the new b (original a) from the new a (which is a+b) to get the original b and assign it to a
// Step 5: Display the swapped values
printf("After swapping, a = %d, b = %d\n", a, b);
return 0; // Step 6: Return 0 to indicate successful execution
}
C Program (Using XOR Operation)
#include <stdio.h>
/**
* C Program to Swap Two Numbers Without Using a Temporary Variable (Using XOR)
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the numbers
int a, b;
// Step 2: Prompt the user to enter the first number
printf("Enter the first number (a): ");
scanf("%d", &a);
// Step 3: Prompt the user to enter the second number
printf("Enter the second number (b): ");
scanf("%d", &b);
// Step 4: Swap the numbers using XOR operation
a = a ^ b; // Step 4.1: XOR a and b, store the result in a
b = a ^ b; // Step 4.2: XOR the new a (which is a^b) with b to get the original a and assign it to b
a = a ^ b; // Step 4.3: XOR the new a (which is a^b) with the new b (original a) to get the original b and assign it to a
// Step 5: Display the swapped values
printf("After swapping, a = %d, b = %d\n", a, b);
return 0; // Step 6: Return 0 to indicate successful execution
}
Explanation
Using Addition and Subtraction
- Step 4.1: Add
aandband store the result ina. Now,aholds the sum ofaandb. - Step 4.2: Subtract
bfrom the new value ofato get the original value ofa, and assign it tob. - Step 4.3: Subtract the new value of
b(which now holds the originala) from the new value ofato get the original value ofb, and assign it toa.
Using XOR Operation
- Step 4.1: XOR
aandb, store the result ina. Nowaholds the XOR ofaandb. - Step 4.2: XOR the new value of
awithbto get the original value ofa, and assign it tob. - Step 4.3: XOR the new value of
a(which is the XOR of originalaandb) with the new value ofb(originala) to get the original value ofb, and assign it toa.
Step 5: Display the Swapped Values
- The swapped values of
aandbare displayed using theprintffunction.
Step 6: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example (Using Addition and Subtraction):
Enter the first number (a): 5
Enter the second number (b): 10
After swapping, a = 10, b = 5
Example (Using XOR Operation):
Enter the first number (a): 5
Enter the second number (b): 10
After swapping, a = 10, b = 5
Conclusion
This C program demonstrates how to swap the values of two variables without using a temporary variable. Both the addition-subtraction method and the XOR operation method are efficient and avoid the need for extra storage, making them useful techniques in various programming scenarios.