Let’s go through both methods for swapping two numbers: using a temporary variable and without using a temporary variable.
Method 1: Swapping Two Numbers Using a Temporary Variable
Step 1: Start
Step 2: Input: Read two integers a and b from the user.
Step 3: Process:
Declare a temporary variable temp.
Assign the value of a to temp.
Assign the value of b to a.
Assign the value of temp to b.
Step 4: Output: Display the values of a and b after swapping.
Step 5: End
C Program (Using a Temporary Variable)
#include <stdio.h>
int main() {
int a, b, temp;
// Step 2: Read two integers from the user
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);
// Step 3: Swap the values using a temporary variable
temp = a; // Store the value of a in temp
a = b; // Assign the value of b to a
b = temp; // Assign the value of temp to b
// Step 4: Output the swapped values
printf("After swapping: a = %d, b = %d\n", a, b);
// Step 5: End
return 0;
}
Explanation of Each Step
Include Header: This line includes the standard input-output library.
Main Function: The main function is the entry point of the C program.
Variable Declaration: Three integer variables a, b, and temp are declared. a and b will store the user inputs, and temp will be used for swapping.
Reading Input:
- printf prompts the user to enter two integers.
- scanf reads the integers and stores them in variables a and b.
Swapping Using a Temporary Variable:
- The value of a is stored in temp.
- The value of b is assigned to a.
- This value is stored in temp (initial value of a) is assigned to b.
Output: This line prints the swapped values of a and b.
End: This line indicates successful completion of the program.
Method 2: Swapping Two Numbers Without Using a Temporary Variable
Step 1: Start
Step 2: Input: Read two integers a and b from the user.
Step 3: Process:
Use arithmetic operations to swap the values.
a = a + b
b = a - b
a = a - b
Step 5: Output: Display the values of a and b after swapping.
Step 6: End
C Program (Without Using a Temporary Variable)
#include <stdio.h>
int main() {
int a, b;
// Step 2: Read two integers from the user
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);
// Step 3: Swap the values without using a temporary variable
a = a + b; // a now holds the sum of a and b
b = a - b; // b now holds the original value of a
a = a - b; // a now holds the original value of b
// Step 4: Output the swapped values
printf("After swapping: a = %d, b = %d\n", a, b);
// Step 5: End
return 0;
}
Explanation of each step:
Include Header: This line includes the standard input-output library.
Main Function: The main function is the entry point of the C program.
Variable Declaration: Two integer variables a and b are declared to store the user inputs.
Reading Input:
- printf prompts the user to enter two integers.
- scanf reads the integers and stores them in variables a and b.
Swapping Without Using a Temporary Variable:
- a = a + b stores the sum of a and b in a.
- b = a – b assigns the original value of a to b (since a now holds a + b, a – b results in the original a).
- a = a – b assigns the original value of b to a (since b now holds the original a).
Output: This line prints the swapped values of a and b.
End: This line indicates successful completion of the program.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.