Beginner · variables

Swap Two Numbers

Printing b then a needs no swap at all — just two couts in the other order. The interesting version is actually swapping them, and that is where beginners lose a value.

At a glancePrint <code>b</code> then <code>a</code> on one line. If you swap in variables instead, save one value in a temporary first.

Problem

Input: Two integers a and b (−1000 ≤ each ≤ 1000), separated by a space.

Output: b then a, separated by a single space.

Samples:

InputOutput
3 55 3
-1 77 -1

Two equally correct strategies: print in the other order (one line, no state change), or actually swap the variables using a temporary (three lines, and the habit you will need for sorting). Both pass — they are different lessons.

Your solution

main.cpp — your solution
Custom input for "Run my input"

Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.

Stuck? Hints

Hint 1 — The simplest answer

cout << b << " " << a << endl; — done. Nothing in the program needs the values swapped; only the output order matters. Recognising when state does not need to change is a real skill.

Hint 2 — Why a real swap needs a third variable

Try a = b; b = a; and both end up holding b's old value — the first assignment already destroyed a. The temporary holds a's value while the first assignment overwrites it: int t = a; a = b; b = t;. Order and the temporary are both load-bearing.

Hint 3 — One cin can read two

cin >> a >> b; reads both values in one statement, splitting on whitespace. You will see this shorthand everywhere — cin >> a; cin >> b; is identical but noisier.

Solution (try it first!)
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;

    int t = a;
    a = b;
    b = t;

    cout << a << " " << b << endl;
    return 0;
}

That three-line swap is exactly what Bubble Sort repeats thousands of times, and what Merge Intervals has to do carefully across two parallel arrays. Learn the order now and it never bites you again.

Deep dive

Why this works

If only the output order matters, printing b then a is complete — no state change needed. If you actually swap, the first assignment destroys one value, so a temporary must save it: t = a; a = b; b = t;. Order and the temporary are both essential.

The cost

O(1) — one read, one write line, or three assignments for a real swap.

Where it goes wrong

  • a = b; b = a; — both variables end up holding b's old value; a's value is gone.
  • Swapping only one of the two (writing a = b; and stopping) — the output has the same number twice.
  • Assuming swap() exists — it does in <algorithm>, which this engine does not provide. Three lines by hand always work.

Next step

The same three lines, repeated in a loop: Bubble Sort and Sort Three Numbers.

Two things to remember

  • A real swap is three assignments; the temporary protects the value you are about to overwrite.
  • If only the output order matters, do not swap at all — just print in the other order.
🐞 This version fails — open it and try

a = b; b = a; — both end up with b's value, a is lost

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    a = b;          // bug: a is overwritten before it is saved
    b = a;
    cout << a << " " << b << endl;
    return 0;
}