Display Multiplication Table in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 2 Code Examples
🚀 Live Preview
Loops

What You’ll Learn

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.

Times Table

n×1 … n×10

One product per row; school-style output.

for Loop

i = 1 … last

Repeat the same formula; only the counter changes.

printf Row

base x i = p

Readable lines without copying ten statements.

Fixed or Input

5 or scanf

Bake in a base, then accept a typed number.

Live Preview

Pick a base

Change the base and print 1–10 in the browser.

O(k)

k rows

Linear in the number of printed lines.

Introduction

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.

Why it matters?

It is the classic first loop drill: fixed count, formatted output, and optional input validation — skills reused for factorials, ranges, and nested tables.

Key Highlights

Base × i

Same formula every row.

Counted Loop

for from 1 to last.

printf Pattern

Readable n x i = product lines.

O(last)

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.

📝 Problem & Approach

Given a base and a last multiplier, print each product on its own line with a clear header.

c
/* base = 5, last = 10
 * 5 x 1 = 5
 * 5 x 2 = 10
 * …
 * 5 x 10 = 50
 */

Inputs & Outputs

ItemTypeDescription
baseintThe number whose times table you print.
lastintLast multiplier (10 in these examples).
outputtext linesHeader plus one base x i = product row per i.

Minimal workflow

Pseudocode
procedure print_table(base, last):
    print header with base
    for i from 1 to last:
        print base, i, and base * i

Method comparison

ApproachWhenNotes
for loop (this page)Known row countClearest for 1…last
while loopSame mathYou update the counter by hand
Ten copied printfsNeverBreaks as soon as last changes

⚡ Quick Reference

GoalPattern
One rowprintf("%d x %d = %d\n", base, i, base * i);
Loopfor (int i = 1; i <= last; ++i)
Headerprintf("Multiplication table for %d:\n", base);
ValidateReject non-positive bases for classic tables
CostO(last) time, O(1) extra space

📋 for vs while vs Hard-Coded Rows

Same products — different ways to repeat the print.

for loop
i=1..last

This page — clearest counted form

while loop
i++; manually

Same results; more bookkeeping

Copied lines
10 printfs

Fragile when last changes

Interview tip
param last

Make row count easy to change

Context

When This Problem Shows Up

Reach for a multiplication-table loop whenever you need repeated formatted products with a fixed counter range.

  1. First loop homework

    Classic intro exercise after printf.

  2. Teaching times tables

    Generate practice sheets programmatically.

  3. scanf warm-up

    Read a base, validate, then print.

  4. Formatting practice

    Align columns with width flags later.

  5. Not a full grid

    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.

🔮 Live Preview

Default base 5 matches Example 1. Change it to any reasonable integer; rows run from 1 to 10.

Runs in your browser. Very large bases may wrap awkwardly in the box—your C program would still compute the product.

Live result
Press “Print table”.

Examples Gallery

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.

📚 Getting Started

Fix a base and last, then loop with printf.

Example 1 — Table for 5 (Fixed Base)

Same spirit as the classic exercise: print 5×1 through 5×10. Uses a small helper and int main(void).

c
#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;
}

How It Works

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 *.

📈 Practical Patterns

Same helper; the base comes from the keyboard.

Example 2 — Table for a Number You Type

Reads one integer and prints 1–10 for that base. Rejects non-positive values so the table stays in the usual times-table style.

c
#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;
}

How It Works

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.

🧠 How the Algorithm Prints the Table

1

Choose base and range

Set base (fixed or from input). Set last (10 in these examples).

Setup
2

Print a header

A friendly title line helps when you compare output to a textbook table.

Header
3

Loop

For i = 1 to last, print base, i, and base * i.

Rows
=

Table ready

For base 5, the last line is 5 x 10 = 50.

🔎 Worked Walkthrough — First Rows for Base 5

Trace how i produces each product when base = 5.

iExpressionPrinted line
15 * 15 x 1 = 5
25 * 25 x 2 = 10
35 * 35 x 3 = 15
75 * 75 x 7 = 35
105 * 105 x 10 = 50

Every other row follows the same pattern until i reaches last.

Use Cases

Where multiplication-table thinking shows up beyond the homework prompt.

1. Counted-Loop Practice

Master for with a known upper bound.

Example: 1 through 10.

2. Formatted Output

Combine several values in one printf.

Example: base, i, product.

3. Input Validation

Check scanf and reject bad bases.

Example: Example 2.

4. Parameterized Helpers

Pass base and last instead of hard-coding.

Example: print_multiplication_table.

5. Stepping Stone

Lead into nested loops for full grids.

Example: interview follow-ups.

6. Complexity Talk

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.

Advantages

Why the looped table approach earns interview points.

  1. 1. No Repeated Code

    One printf inside the loop covers every row.

  2. 2. Easy to Resize

    Change last to 12 without rewriting lines.

  3. 3. Clear Complexity

    O(last) is the expected answer.

  4. 4. Reusable Helper

    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.

Usage Tips

Small habits that keep multiplication-table code clean in interviews.

  1. 1. Parameterize last

    Pass the upper bound instead of burying 10 everywhere.

  2. 2. Loop Inclusive

    Use i <= last so row 10 is included.

  3. 3. Check scanf

    Require scanf(...) == 1 before using the base.

  4. 4. Clarify the Letter x

    It is display text; multiplication uses *.

  5. 5. Quote O(last)

    Complexity tracks the number of printed rows.

Pro Tip: dry-run i = 1, 2, and last on paper so inclusive bounds stay correct.

Common Pitfalls

Mistakes that commonly break multiplication-table solutions in C.

  1. 1. Off-by-One on last

    Using i < last skips the final row (no 10).

    → Prefer i <= last for inclusive tables.

  2. 2. Ignoring scanf Failure

    Using an uninitialized n when input is garbage.

    → Check that scanf returns 1.

  3. 3. Copying Ten printfs

    Breaks the moment the teacher asks for 1–12.

    → Use a loop with a last parameter.

  4. 4. Confusing x with *

    Writing invalid C like 5 x 7 as an expression.

    → Multiply with *; print the letter x only as text.

  5. 5. Silent Non-Positive Bases

    Homework tables usually expect a positive base.

    → Validate or document that negatives are allowed.

Edge Cases

Check these before calling the solution done.

Bad input

scanf fails

Return an error message instead of using garbage as the base.

Zero / negative

Non-positive base

Decide whether to reject (as here) or allow—classic tables usually use a positive base.

last

last < 1

The loop body never runs; print nothing (or reject).

1 row

last == 1

Only base x 1 = base appears—still a valid table.

Overflow

Huge products

For very large bases or last, consider long long and %lld.

Format

Column alignment

Optional polish with widths like %3d for tidier grids.

🔄 Input / Output

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.

SampleHighlight
Fixed base 5Ends with 5 x 10 = 50
Typed base 4Ends with 4 x 10 = 40

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Table of 7 through 12

  • Set base = 7 and last = 12
  • Confirm the last line is 84

2. Read last too

  • Ask for base and last
  • Validate both are positive

3. Align columns

  • Use width flags in printf
  • Keep products lined up

4. Nested grid

  • Print bases 1–5 as nested loops
  • Stretch goal after this page

Notes

  • Core loop: for each i, print base * i.
  • Cost: linear in the number of rows printed.
  • Stretch: read base (and maybe last) from the user with validation.
  • The printed letter 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.

⏱️ Time and Space Complexity

TaskTimeExtra space
Print last rowsO(last)O(1)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Use a loop with an inclusive upper bound
  • Pass base and last into a helper
  • Check scanf before using input
  • Reject non-positive bases for classic tables
  • Quote O(last) for the printed rows

❌ Don’t

  • Copy ten separate printf lines
  • Use i < last when you need row 10
  • Treat the letter x as the C multiply operator
  • Ignore failed scanf results
  • Forget to discuss validation in interviews

Key Takeaways

Knowledge Unlocked

Five things to remember about multiplication tables in C

Implement it the interview-friendly way.

5
Core concepts
1 02

Loop

1 … last

Control
P 03

Print

One row

I/O
? 04

Input

Validate

scanf
O 05

Cost

O(last)

Analysis

❓ Frequently Asked Questions

It is a neat list of answers for “n times 1,” “n times 2,” and so on—usually up to 10 in homework-style programs. Each row is one multiply.
The pattern repeats: same formula (base times counter), only the counter changes. A loop writes every row without copying the printf line ten times.
School tables often go 1–10. You can change the upper limit to 12 or any positive integer; it is just a design choice.
Yes. A for loop is convenient when you know how many steps you want; a while loop can do the same with a counter you update by hand.
Multiplication still works in C, but a “times table” for non-positive bases is unusual. Example 2 checks for positive input; you can tighten rules for your assignment.
Printing k rows costs Θ(k) time and O(1) extra space besides the output lines.
No. In printf it is just text so the line reads like “5 x 7 = 35”. The real multiply in C is the * operator.
Pass a different last value to the helper (for example 12) or read last from the user.

Did you Know? 🔊

A 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.

Continue to Natural Number

Learn how to check whether a number is a natural (positive) integer in C.

Natural Number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful