Beginner · loops & output

Multiplication Table

Read N and print its times table, one line per row. Simple loop — but the judge checks the format character by character, which is exactly the skill this problem teaches.

At a glancePrint N lines of "N x i = product" for i from 1 to N — the only real difficulty is the exact spacing, because the judge compares output character by character.

Problem

Input: one integer N (1 ≤ N ≤ 10).

Output: N lines, line i reading exactly N x i = product (spaces as shown).

Sample: for N = 3:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9

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 loop is the easy part

for (int i = 1; i <= n; i++) — same skeleton as Sum from 1 to N. The actual work is one cout line per pass.

Hint 2 — match the spaces exactly

cout << n << " x " << i << " = " << n * i << endl; — note the spaces inside the quoted strings: " x " and " = ". Most failed attempts on this problem are missing spaces, not logic.

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

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cout << n << " x " << i << " = " << n * i << endl;
    }
    return 0;
}

Formatting output to spec is a real, underrated skill — log files, reports and judges all demand it. Want the 2D version (the full N×N table)? That's nested loops — see Star Pyramid next, and the loops guide if the skeleton still feels shaky.

Deep dive

Why this works

The judge compares output byte by byte, so this problem is really about formatting. The loop is trivial; the spacing is the lesson.

The cost

O(n²) output — you cannot print faster than the characters you emit.

Where it goes wrong

  • This judge trims trailing whitespace, so a stray space at the end of a line survives here — most online judges do not trim. What must be exact is the spacing between tokens.
  • endl flushes the stream on every value. Correct here, but '\n' is the habit that scales.

Next step

Formatting with leading spaces instead: Star Pyramid.

Two things to remember

  • Judges compare bytes, not appearance.
  • Trailing whitespace is the number-one silent failure.
🐞 This version fails — open it and try

Prints 3x1=3 — the spaces around x and = are gone

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cout << n << "x" << i << "=" << n * i << endl;
    }
    return 0;
}