Times Table
n×1 … n×10
One product per row; school-style output.
A multiplication table (times table) for a base n is the list of products n×1, n×2, … — usually through 10. This tutorial covers a clear for loop with printf, a live preview you can retarget, worked C examples (fixed base 5 and scanf input), edge cases, and O(k) complexity for k rows.
n×1 … n×10
One product per row; school-style output.
i = 1 … last
Repeat the same formula; only the counter changes.
base x i = p
Readable lines without copying ten statements.
5 or scanf
Bake in a base, then accept a typed number.
Pick a base
Change the base and print 1–10 in the browser.
k rows
Linear in the number of printed lines.
A multiplication table for a base number n lists n×1, n×2, … up to a chosen last multiplier (often 10). Each line is one multiply — what you practiced as times tables in school, now printed by a loop.
You pick one number (the base). For each step i from 1 up to 10, print base × i. That is the whole program idea — no arrays needed for the basic version.
It is the classic first loop drill: fixed count, formatted output, and optional input validation — skills reused for factorials, ranges, and nested tables.
Same formula every row.
for from 1 to last.
Readable n x i = product lines.
Cost grows with rows printed.
In short: for i from 1 to last, print base, i, and base * i — change last if you need 1–12.
Given a base and a last multiplier, print each product on its own line with a clear header.
/* base = 5, last = 10
* 5 x 1 = 5
* 5 x 2 = 10
* …
* 5 x 10 = 50
*/ | Item | Type | Description |
|---|---|---|
base | int | The number whose times table you print. |
last | int | Last multiplier (10 in these examples). |
| output | text lines | Header plus one base x i = product row per i. |
procedure print_table(base, last):
print header with base
for i from 1 to last:
print base, i, and base * i | Approach | When | Notes |
|---|---|---|
for loop (this page) | Known row count | Clearest for 1…last |
while loop | Same math | You update the counter by hand |
| Ten copied printfs | Never | Breaks as soon as last changes |
| Goal | Pattern |
|---|---|
| One row | printf("%d x %d = %d\n", base, i, base * i); |
| Loop | for (int i = 1; i <= last; ++i) |
| Header | printf("Multiplication table for %d:\n", base); |
| Validate | Reject non-positive bases for classic tables |
| Cost | O(last) time, O(1) extra space |
Same products — different ways to repeat the print.
i=1..lastThis page — clearest counted form
i++; manuallySame results; more bookkeeping
10 printfsFragile when last changes
param lastMake row count easy to change
Reach for a multiplication-table loop whenever you need repeated formatted products with a fixed counter range.
Classic intro exercise after printf.
Generate practice sheets programmatically.
Read a base, validate, then print.
Align columns with width flags later.
This prints one base’s table, not an n×n chart (that needs nested loops).
Key benefit: one tiny loop that proves you can combine counting, multiplication, and formatted output without repetition.
Default base 5 matches Example 1. Change it to any reasonable integer; rows run from 1 to 10.
Two complete C programs — a fixed table for 5, then the same helper with scanf and validation. Click View Output to reveal sample console results.
Fix a base and last, then loop with printf.
Same spirit as the classic exercise: print 5×1 through 5×10. Uses a small helper and int main(void).
#include <stdio.h>
void print_multiplication_table(int base, int last) {
printf("Multiplication table for %d:\n", base);
for (int i = 1; i <= last; ++i) {
printf("%d x %d = %d\n", base, i, base * i);
}
}
int main(void) {
const int base = 5;
const int last = 10;
print_multiplication_table(base, last);
return 0;
} last controls how many rows you print—change it to 12 if your teacher wants a “1 through 12” table. The letter x in the output is just text; it is not the variable x from algebra. The real multiply is *.
Same helper; the base comes from the keyboard.
Reads one integer and prints 1–10 for that base. Rejects non-positive values so the table stays in the usual times-table style.
#include <stdio.h>
void print_multiplication_table(int base, int last) {
printf("Multiplication table for %d:\n", base);
for (int i = 1; i <= last; ++i) {
printf("%d x %d = %d\n", base, i, base * i);
}
}
int main(void) {
int n;
printf("Enter a positive integer: ");
if (scanf("%d", &n) != 1) {
printf("Could not read an integer.\n");
return 1;
}
if (n <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
print_multiplication_table(n, 10);
return 0;
} Check scanf’s return value before using n. Rejecting n <= 0 keeps the program in homework-style territory; C can still multiply zeros and negatives if you choose to allow them.
Set base (fixed or from input). Set last (10 in these examples).
A friendly title line helps when you compare output to a textbook table.
For i = 1 to last, print base, i, and base * i.
For base 5, the last line is 5 x 10 = 50.
Trace how i produces each product when base = 5.
| i | Expression | Printed line |
|---|---|---|
1 | 5 * 1 | 5 x 1 = 5 |
2 | 5 * 2 | 5 x 2 = 10 |
3 | 5 * 3 | 5 x 3 = 15 |
7 | 5 * 7 | 5 x 7 = 35 |
10 | 5 * 10 | 5 x 10 = 50 |
Every other row follows the same pattern until i reaches last.
Where multiplication-table thinking shows up beyond the homework prompt.
Master for with a known upper bound.
Example: 1 through 10.
Combine several values in one printf.
Example: base, i, product.
Check scanf and reject bad bases.
Example: Example 2.
Pass base and last instead of hard-coding.
Example: print_multiplication_table.
Lead into nested loops for full grids.
Example: interview follow-ups.
Quote O(k) for k printed rows.
Example: last = 10 or 12.
Pro Tip: say “for i from 1 to last, print base * i” before coding — then mention validation if input is involved.
Why the looped table approach earns interview points.
One printf inside the loop covers every row.
Change last to 12 without rewriting lines.
O(last) is the expected answer.
Fixed and interactive programs share the same function.
Pro Tip: mention column alignment (%3d) only as a polish follow-up after the correct loop works.
Small habits that keep multiplication-table code clean in interviews.
Pass the upper bound instead of burying 10 everywhere.
Use i <= last so row 10 is included.
Require scanf(...) == 1 before using the base.
It is display text; multiplication uses *.
Complexity tracks the number of printed rows.
Pro Tip: dry-run i = 1, 2, and last on paper so inclusive bounds stay correct.
Mistakes that commonly break multiplication-table solutions in C.
Using i < last skips the final row (no 10).
→ Prefer i <= last for inclusive tables.
Using an uninitialized n when input is garbage.
→ Check that scanf returns 1.
Breaks the moment the teacher asks for 1–12.
→ Use a loop with a last parameter.
Writing invalid C like 5 x 7 as an expression.
→ Multiply with *; print the letter x only as text.
Homework tables usually expect a positive base.
→ Validate or document that negatives are allowed.
Check these before calling the solution done.
scanf failsReturn an error message instead of using garbage as the base.
Decide whether to reject (as here) or allow—classic tables usually use a positive base.
last < 1The loop body never runs; print nothing (or reject).
last == 1Only base x 1 = base appears—still a valid table.
For very large bases or last, consider long long and %lld.
Optional polish with widths like %3d for tidier grids.
Example 1 needs no input. Example 2 reads one integer from standard input; you can switch to a fixed value while learning, then add scanf when you are ready.
| Sample | Highlight |
|---|---|
| Fixed base 5 | Ends with 5 x 10 = 50 |
| Typed base 4 | Ends with 4 x 10 = 40 |
Try these variations to lock in the pattern.
i, print base * i.base (and maybe last) from the user with validation.x is text; C multiplies with *.Quick Takeaway: loop i from 1 to last, print base * i each time, and validate input when the base comes from the user.
| Task | Time | Extra space |
|---|---|---|
Print last rows | O(last) | O(1) |
A multiplication table is a counted loop of products: choose a base, walk i from 1 to last, and print each line with printf. Master the fixed table of 5, then add scanf with positive-input checks.
Practice both examples above, then continue to natural numbers for a related integer-check warm-up.
For each i in 1…last, print base * i — and validate the base when it comes from input.
base and last into a helperscanf before using inputO(last) for the printed rowsi < last when you need row 10x as the C multiply operatorscanf resultsImplement it the interview-friendly way.
base * i
Math1 … last
ControlOne row
I/OValidate
scanfO(last)
AnalysisA multiplication table for a number n is just the list of products n×1, n×2, … . Each line is one multiplication—what you practiced as “times tables” in school—now printed by a loop instead of by hand.
Learn how to check whether a number is a natural (positive) integer in C.
8 people found this page helpful