how to algorithm of swap the contents of two variable using a third variable
1. Declare three variables a, b, and c.
2. Assign values to a and b.
3. Print the initial values of a and b.
4. Assign the value of a to c.
5. Assign the value of b to a.
6. Assign the value of c to b.
7. Print the final values of a and b.
8. End.
Pseudo code:
1. Declare variables a, b, and c
2. Assign values to a and b
3. Print the initial values of a and b
4. Assign the value of a to c
5. Assign the value of b to a
6. Assign the value of c to b
7. Print the final values of a and b
8. End
To swap the contents of two variables using a third variable, follow these steps:
Step 1: Declare the three variables
- Declare the variables `a`, `b`, and `temp`.
Step 2: Assign values to `a` and `b`
- Assign values to both `a` and `b` variables.
Step 3: Initialize `temp` with the value of `a`
- Assign the value of `a` to `temp`.
Step 4: Assign the value of `b` to `a`
- Assign the value of `b` to `a`.
Step 5: Assign the value of `temp` to `b`
- Assign the value of `temp` to `b`.
Here's an implementation example in Python:
```python
# Step 1: Declare the three variables
a = 10
b = 5
temp = 0
# Step 2: Assign values to a and b
print("Before swapping:")
print("a =", a)
print("b =", b)
# Step 3: Initialize temp with the value of a
temp = a
# Step 4: Assign the value of b to a
a = b
# Step 5: Assign the value of temp to b
b = temp
# Print the swapped values
print("After swapping:")
print("a =", a)
print("b =", b)
```
Output:
```
Before swapping:
a = 10
b = 5
After swapping:
a = 5
b = 10
```
After following these steps, the values of `a` and `b` will be swapped using the third variable `temp`.