Swap Two Numbers: flowchart, algorithm and program
This program reads two integers, a and b, swaps their values using a third variable called temp, and prints both. The idea is to park one value in temp so it isn't overwritten when the other value is copied over it.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read a.
- Step 3: Read b.
- Step 4: Set temp = a.
- Step 5: Set a = b.
- Step 6: Set b = temp.
- Step 7: Print "After swapping".
- Step 8: Print a, b.
- Step 9: Stop.
Pseudocode
BEGIN
READ a
READ b
temp ← a
a ← b
b ← temp
PRINT "After swapping"
PRINT a, b
END
Program in C, Python and Java
Swap Two Numbers program in C
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}Swap Two Numbers program in Python
a = int(input("Enter a: "))
b = int(input("Enter b: "))
temp = a
a = b
b = temp
print("After swapping:")
print("a =", a)
print("b =", b)Swap Two Numbers program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
int temp = a;
a = b;
b = temp;
System.out.println("After swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Sample input: a = 4, b = 6
Output: Enter a: Enter b: After swapping:
a = 6
b = 4
How it works
- Read the two integers into a and b with scanf. For the sample input, a = 4 and b = 6.
- temp = a; saves the original value of a (4) in temp, so it is safe.
- a = b; copies b into a, so a becomes 6. The old value of a is not lost because temp holds it.
- b = temp; puts the saved value into b, so b becomes 4.
- Print a and b. The output is a = 6 and b = 4, the values are swapped.
Dry run (trace table)
| Step | Line | Box | a | b | temp | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 7 | Read a | 4 | Enter a: | ||
| 3 | 9 | Read b | 4 | 6 | Enter b: | |
| 4 | 11 | temp = a | 4 | 6 | 4 | |
| 5 | 12 | a = b | 6 | 6 | 4 | |
| 6 | 13 | b = temp | 6 | 4 | 4 | |
| 7 | 15 | Print "After swapping" | 6 | 4 | 4 | After swapping: |
| 8 | 16 | Print a, b | 6 | 4 | 4 | a = 6 b = 4 |
| 9 | 19 | Stop | 6 | 4 | 4 |
Time and space complexity
Time: O(1) · Space: O(1). Only a fixed number of assignments are done, using one extra variable.
Common mistakes
- Writing a = b; b = a; without temp. After a = b, the old value of a is gone, so both variables end up with the same value (6 and 6).
- Doing the three assignments in the wrong order, such as a = b; temp = a; b = temp; which also loses the original a.
- Forgetting the & in scanf("%d", &a); so the input is not stored in the variable.
Viva questions
Q1. Why do we need the variable temp in this program?
If we wrote a = b first, the old value of a would be overwritten and lost. temp holds a's original value so we can later give it to b.
Q2. Walk me through lines 11 to 13 with a = 5 and b = 9.
temp = a makes temp 5; a = b makes a 9; b = temp makes b 5. So a is 9 and b is 5, and the values are swapped.
Q3. What happens if you change the order of the three statements, for example a = b; temp = a; b = temp;?
The swap breaks. After a = b both a and b hold the old b, so temp also gets the old b and both variables end up equal to b.
Q4. What is the purpose of & in scanf("%d", &a)?
scanf needs the address of the variable so it can store the typed value directly into a's memory location. Without & it would receive the value instead of an address, which is wrong and can crash.
Q5. What happens if the user enters the same value for a and b, or enters 0 or negative numbers?
The swap still works: with equal values nothing visibly changes, and 0 or negative integers swap like any other int because %d reads signed integers.
Q6. What is the time complexity of this program, and how much extra space does it use?
It is O(1) time because it runs a fixed number of statements regardless of the input values. Extra space is O(1) too, just the one temp variable.
Q7. Can you swap two numbers without a temporary variable? What would change in the code?
Yes, for example with arithmetic: a = a + b; b = a - b; a = a - b; or with XOR: a = a ^ b; b = a ^ b; a = a ^ b. The addition version can overflow for large ints, so the temp method is the safest and clearest.
Q8. If you wanted to put the swap in a separate function, what would you need to change?
You must pass the addresses, as in swap(&a, &b), and use pointers inside the function. Passing by value in C would only swap copies, leaving the original a and b unchanged.
Frequently asked questions
Why do we need a temporary variable to swap two numbers?
When you assign a = b, the old value of a is overwritten. temp keeps a copy of that old value so you can give it to b afterwards.
Can you swap two numbers without a third variable?
Yes, you can use arithmetic (a = a + b; b = a - b; a = a - b;) or XOR. The temp method is clearer and has no overflow risk, so it is the one usually taught first.
What is the dry run of this swap program for a = 4 and b = 6?
Start: a=4, b=6. After temp = a: temp=4. After a = b: a=6. After b = temp: b=4. The program prints a = 6 and b = 4.