Swap Without a Third Variable: flowchart, algorithm and program

This program reads two integers, a and b, and swaps their values without using a temporary variable. It stores the sum in a, then uses subtraction twice to recover each original value in the other variable.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read a.
  3. Step 3: Read b.
  4. Step 4: Set a = a + b.
  5. Step 5: Set b = a - b.
  6. Step 6: Set a = a - b.
  7. Step 7: Print a, b.
  8. Step 8: Stop.

Pseudocode

BEGIN
    READ a
    READ b
    a ← a + b
    b ← a - b
    a ← a - b
    PRINT a, b
END

Program in C, Python and Java

Swap Without a Third Variable program in C

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter a: ");
    scanf("%d", &a);
    printf("Enter b: ");
    scanf("%d", &b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}

Swap Without a Third Variable program in Python

a = int(input("Enter a: "))
b = int(input("Enter b: "))

a = a + b
b = a - b
a = a - b

print("After swapping: a =", a, ", b =", b)

Swap Without a Third Variable 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();

        a = a + b;
        b = a - b;
        a = a - b;

        System.out.println("After swapping: a = " + a + ", b = " + b);
    }
}

Sample input: a = 4, b = 6
Output: Enter a: Enter b: After swapping: a = 6, b = 4

How it works

  • After reading input, a = 4 and b = 6. The line a = a + b makes a hold the sum of both numbers, so a = 10.
  • The line b = a - b computes 10 - 6 = 4. That is the original a, so b now holds the old value of a.
  • The line a = a - b computes 10 - 4 = 6. That is the original b, so a now holds the old value of b.
  • No third variable is needed because the sum in a keeps both original values until each one is pulled back out by subtraction.
  • Finally printf shows a = 6, b = 4, which are the two input values exchanged.

Dry run (trace table)

StepLineBoxabOutput
13Start
26Read a4Enter a:
38Read b46Enter b:
410a = a + b106
511b = a - b104
612a = a - b64
714Print a, b64After swapping: a = 6, b = 4
815Stop64

Time and space complexity

Time: O(1) · Space: O(1). Only a fixed number of arithmetic operations are done, with no loops or extra storage.

Common mistakes

  • Changing the order of the three statements. For example, doing a = a - b before b = a - b gives wrong results, because the sum in a must be used first.
  • Forgetting that a + b can overflow an int when the numbers are very large, which makes the result undefined in C. For big values, a temporary variable is safer.
  • Using the same variable for both inputs or swapping a variable with itself through pointers. With the sum trick, that sets the value to 0 instead of keeping it.

Viva questions

Q1. What does this program do, and how does it swap a and b without a third variable?

It swaps the values of a and b using only addition and subtraction. First a holds the sum, then b gets the old a, and finally a gets the old b.

Q2. Trace the program with a = 5 and b = 3. What are the values after each of lines 10, 11 and 12?

After line 10, a = 8 and b = 3. After line 11, b = 8 - 3 = 5. After line 12, a = 8 - 5 = 3. So a = 3 and b = 5, which is swapped.

Q3. What is stored in a after line 10, and why is it important?

a holds a + b, the sum of both original values. This keeps both numbers recoverable. Subtracting one of them gives back the other.

Q4. What happens if both inputs are the same, for example a = 4 and b = 4, or if one is 0?

It still works. With 4 and 4, a becomes 8, then b = 4, then a = 4. With 0 the sum is just the other number, and the swap is still correct.

Q5. What is the risk of this method for very large integers?

a + b can overflow the int range, for example 2 billion plus 2 billion. Signed overflow is undefined behaviour in C, so the result may be wrong. For small values it is fine.

Q6. What is the time and space complexity of this program?

Time is O(1) because there are only three arithmetic statements and no loops. Space is O(1) because it uses just two variables.

Q7. What would change if you wanted to swap using XOR instead of + and -?

You would write a = a ^ b; b = a ^ b; a = a ^ b;. It has no overflow problem because XOR never carries. It works only on integer types.

Q8. Why is it better in practice to use a temporary variable, and what would that code look like?

A temp variable is clearer and has no overflow risk: temp = a; a = b; b = temp;. The compiler usually optimises it well. The no-temp trick is mainly a lab or interview exercise.

Frequently asked questions

How do you swap two numbers without a third variable?

Set a = a + b, then b = a - b, then a = a - b. After these three steps the values of a and b are exchanged.

Is swapping with addition and subtraction better than using a temp variable?

Not really. It saves one variable but can overflow for large integers and is harder to read, so a temp variable is usually the better choice in real code.

Can I swap without a third variable in another way?

Yes. You can use XOR: a = a ^ b; b = a ^ b; a = a ^ b. It avoids overflow, but it also fails if both operands are the same memory location.

More programs

← All programs

Try AI Flowchart free →