Definition
Times table
Rows of products for one base number.
A times table is a short loop: for each row index i, print base x i = base * i. This tutorial covers fixed and interactive bases, a custom row limit, a live preview, worked C++ examples, edge cases, and complexity.
Times table
Rows of products for one base number.
i = 1..last
Repeat the same print pattern for each row.
base x i =
Clear output rows that match school tables.
Validate n
Read a positive integer and reject bad input when needed.
Any base
Print rows 1 through 10 for a base you choose.
O(1) space
Linear in the number of printed rows.
A multiplication table for a base n is the list of products n x 1, n x 2, and so on up to some limit, often 10. In code, that is a single loop that prints one formatted row per multiplier.
Interviews use this to check loops, string formatting, and simple input validation, not fancy math. Once the helper exists, you can swap a fixed base for user input or change how many rows you print.
It is one of the cleanest ways to show you understand for loops, formatted output, and readable console programs.
Each i prints one product row.
Reuse printMultiplicationTable.
Catch bad input and non-positive bases.
Pass last as 10, 12, or 20.
In short: for i from 1 to last, print base x i = base * i.
Given a base number and a row limit, print each product from 1 through that limit in a readable format.
// base = 5, last = 10
// 5 x 1 = 5
// 5 x 2 = 10
// ...
// 5 x 10 = 50 | Item | Type | Description |
|---|---|---|
base | int | The number whose table you print, often positive. |
last | int | How many rows to print, commonly 10. |
| Output | text | One line per row: base x i = product. |
procedure PrintTable(base, last):
print heading
for i from 1 to last:
print base, i, and base * i | Method | Idea | Notes |
|---|---|---|
for | Known row count | Interview default, clearest |
while | Increment a counter | Works, slightly more boilerplate |
| Hard-coded prints | Ten separate lines | Avoid, does not scale with last |
| Goal | Pattern |
|---|---|
| Loop rows | for (int i = 1; i <= last; i++) |
| Print row | std::cout << base << " x " << i << " = " << (base * i) << "\\n"; |
| Fixed demo | base = 5, last = 10 |
| Read base | std::cin >> n with fail check |
| Reject bad input | !(std::cin >> n) / if (n <= 0) |
| Align columns | std::setw / stream formatting |
Same table, different ways to drive the rows.
for (int i = 1; ...)This page, clearest for fixed counts
int i = 1; while (i <= last)Fine alternate, remember to increment
10 print linesBreaks as soon as last changes
helper(base, last)Reusable method beats one-off scripts
Reach for a times-table loop whenever you need repeated formatted product rows.
First programs that combine loops and print.
Print the table for a number the user types.
Build readable rows with concatenation or string formatting.
Handle invalid text and non-positive bases.
One base only, nested loops are a different problem.
Key benefit: a tiny reusable helper that teaches loops, formatting, and validation in one place.
Default base is 5 to match Example 1. Change it and click Print table.
Three complete C++ programs: fixed table for 5, user-entered base, and a custom row limit with a while loop. Click View Output to reveal sample console results.
A reusable helper and the classic 5-times table.
Classic 5-times table from 1 to 10.
#include <iostream>
void printMultiplicationTable(int baseNum, int last) {
std::cout << "Multiplication table for " << baseNum << ":\n";
for (int i = 1; i <= last; i++) {
std::cout << baseNum << " x " << i << " = " << (baseNum * i) << "\n";
}
}
int main() {
int baseNum = 5;
int last = 10;
printMultiplicationTable(baseNum, last);
return 0;
} The loop visits multipliers 1 through 10. Each iteration prints one formatted product line for base 5.
Read a positive integer and print its table up to 10.
Reads a positive integer and prints its table up to 10.
#include <iostream>
void printMultiplicationTable(int baseNum, int last) {
std::cout << "Multiplication table for " << baseNum << ":\n";
for (int i = 1; i <= last; i++) {
std::cout << baseNum << " x " << i << " = " << (baseNum * i) << "\n";
}
}
int main() {
std::cout << "Enter a positive integer: ";
int n;
if (!(std::cin >> n)) {
std::cout << "Could not read an integer.\n";
return 0;
}
if (n <= 0) {
std::cout << "Please enter a positive integer.\n";
return 0;
}
printMultiplicationTable(n, 10);
return 0;
} Invalid input becomes a clear message through std::cin fail check. Non-positive values are rejected before printing, matching typical school-table rules.
Let the caller choose how many rows to print, here with a while loop.
Prints the 7-times table through 12 using a while counter.
#include <iostream>
void printMultiplicationTableWhile(int baseNum, int last) {
std::cout << "Multiplication table for " << baseNum
<< " (up to " << last << "):\n";
int i = 1;
while (i <= last) {
std::cout << baseNum << " x " << i << " = " << (baseNum * i) << "\n";
i++;
}
}
int main() {
printMultiplicationTableWhile(7, 12);
return 0;
} A while loop needs an explicit counter and i++ each pass. Prefer for when the row count is known; use while when the stop condition is more open-ended.
Fix them in code or read them from input.
Walk each multiplier in order.
product = base * i for the current row.
Output base x i = product, then continue.
Trace the first few rows for base = 5, last = 10.
| i | base * i | Printed row |
|---|---|---|
1 | 5 | 5 x 1 = 5 |
2 | 10 | 5 x 2 = 10 |
3 | 15 | 5 x 3 = 15 |
… | … | same pattern |
10 | 50 | 5 x 10 = 50 |
After i = 10, the loop ends, matching Example 1.
Where printing a times table shows up beyond the interview prompt.
First clean for-loop with row output.
Example: table of 5.
User types a base, you print 1 through 10.
Example: Example 2 flow.
Practice concatenation and aligned output.
Example: format alignment widths.
Reject bad text and non-positive bases.
Example: std::cin fail check.
Same output, two loop styles.
Example: Example 3.
Another loop that multiplies repeatedly.
Example: related topic.
Pro Tip: write the helper first with fixed args, then wrap it with input, it is easier to debug.
Why the loop-based table approach works well for beginners and interviews.
A few lines that anyone can dry-run on paper.
Change 10 to 12 or 20 without rewriting prints.
One helper serves fixed demos and input programs.
O(last) time and O(1) extra memory.
Pro Tip: prefer for in interviews unless asked specifically for while.
Small habits that keep times-table programs interview-ready.
Keep printing in printMultiplicationTable(base, last).
Remember i <= last is inclusive of the last row.
Handle invalid input and non-positive bases before the loop.
Do not hard-code 10 inside the helper if assignments vary.
Use format alignment widths for neat columns in demos.
Pro Tip: dry-run 5 x 1 through 5 x 3 aloud, if those rows match, the loop is correct.
Mistakes that commonly break times-table programs.
Using i < last and missing the final row.
→ Use i <= last.
Infinite loop when i never increases.
→ Always increment inside while.
Calling int.Parse on non-numeric text crashes.
→ Use a std::cin fail check first.
Copy-pasted lines that cannot change last.
→ Always use a loop.
Nesting loops when only one base was asked.
→ One loop is enough for one times table.
Handle these before claiming the table program is complete.
Catch input errors and show a clear message.
Decide whether to allow or reject based on assignment rules.
The loop prints nothing, validate if that is unexpected.
Still O(last), output volume grows with rows.
Products are all 0, math works, school rules may reject it.
Products flip sign, allow only if the problem says so.
Handy follow-ups interviewers sometimes ask.
last either way.Try these variations to lock in the pattern.
last = 10n <= 0i++base * i for each row index.Quick Takeaway: loop i from 1 to last, print base x i = base * i, validate input when needed.
| Task | Time | Extra space |
|---|---|---|
Print last rows | O(last) | O(1) |
| Input + print | O(last) | O(1) |
| while version | O(last) | O(1) |
Ignoring the size of printed text, cost grows with how many rows you emit.
A multiplication table is a short loop that prints base x i = base * i for each multiplier. Prefer a reusable helper, validate interactive input, and parameterize last when assignments ask for 12 or 20 rows.
Practice the three examples above, then continue to checking whether a number is a natural number.
for (int i = 1; i <= last; i++) print base x i = base * i.
base and lastfor for known row countsbase x i = rowsi++ in whilePrint a times table the interview-friendly way.
One row per i
Patternbase x i =
OutputValidate n
SafetyParameterize rows
FlexibleO(last) / O(1)
AnalysisA multiplication table for a number n is the list n x 1, n x 2, .... A loop prints these rows automatically instead of writing each line by hand.
Learn how to check whether a number is a natural number in C++.
8 people found this page helpful