C Program to Print Multiplication Table – Single Table and N×N Grid

A multiplication table in C is printed using a for loop that multiplies a given number by each integer from 1 to 10. For a full N×N grid, two nested loops are used: the outer loop for the row, the inner loop for the column, with i * j at the intersection. This is one of the first programs beginners write after Hello World because it reinforces loop syntax, printf formatting, and the relationship between a loop variable and printed output.

C Program: Single Multiplication Table

/* Print multiplication table for a given number
 * Compile: gcc -ansi -Wall -Wextra table.c -o table */
#include <stdio.h>

int main(void)
{
    int n, i;
    printf("Enter a number: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input.\n"); return 1;
    }
    printf("Multiplication table for %d:\n", n);
    printf("---------------------------\n");
    for (i = 1; i <= 10; i++)
        printf("%3d x %2d = %4d\n", n, i, n * i);
    return 0;
}

Sample Output

Enter a number: 7
Multiplication table for 7:
---------------------------
  7 x  1 =    7
  7 x  2 =   14
  7 x  3 =   21
  7 x  4 =   28
  7 x  5 =   35
  7 x  6 =   42
  7 x  7 =   49
  7 x  8 =   56
  7 x  9 =   63
  7 x 10 =   70

Code Explanation

  • for (i = 1; i <= 10; i++) — the loop variable i serves as the multiplier. On each iteration, n * i gives the product for that row. Starting at 1 and ending at 10 produces the standard 10-row table.
  • Format specifiers for alignment: %3d prints n right-aligned in a 3-character field, %2d prints i in 2 characters, and %4d prints the product in 4 characters. Without width specifiers, values of different digit counts would produce ragged output.
  • Input validation: Checking scanf("%d", &n) != 1 guards against non-numeric input. If the user types a letter, scanf returns 0 (no items converted) and the program exits cleanly instead of printing a table for an uninitialized variable.

C Program: Full N×N Multiplication Grid

/* Print a full N×N multiplication grid
 * Compile: gcc -ansi -Wall -Wextra grid.c -o grid */
#include <stdio.h>

int main(void)
{
    int n, i, j;
    printf("Enter grid size (2-12): ");
    if (scanf("%d", &n) != 1 || n < 2 || n > 12) {
        printf("Enter a value between 2 and 12.\n"); return 1;
    }
    /* header row */
    printf("     ");
    for (j = 1; j <= n; j++) printf("%4d", j);
    printf("\n     ");
    for (j = 1; j <= n; j++) printf("----");
    printf("\n");
    /* data rows */
    for (i = 1; i <= n; i++) {
        printf("%3d |", i);
        for (j = 1; j <= n; j++)
            printf("%4d", i * j);
        printf("\n");
    }
    return 0;
}

Sample Output (5×5 grid)

Enter grid size (2-12): 5
         1   2   3   4   5
     --------------------
  1 |   1   2   3   4   5
  2 |   2   4   6   8  10
  3 |   3   6   9  12  15
  4 |   4   8  12  16  20
  5 |   5  10  15  20  25

Code Explanation — Grid Version

  • Two nested loops: The outer loop (i) controls rows; the inner loop (j) controls columns. The product at position (i, j) is i * j. Every multiplication table grid you have ever seen on paper is literally this — two nested counters multiplied together.
  • Header row: Printed before the data loops, using a separate loop for the column labels. The separator line ("----" per column) makes the table readable. The printf(" ") call adds 5 spaces to align the header with the row labels.
  • %4d for all values: The largest value in an n×n grid is n². For n=12, that is 144 — three digits. A field width of 4 accommodates up to 9999, which is more than enough for any practical grid size.
  • Why limit to n=12: Standard multiplication tables go to 12. Beyond 12, the values grow quickly and the formatted grid becomes wider than most terminals. The validation n < 2 || n > 12 keeps the output readable.

What This Program Teaches

  • Loop variable as both counter and value: i in the single-table program is both the loop counter and the multiplier printed in the output. Recognizing that the loop variable is data — not just a counter — is a key shift in thinking for beginners.
  • printf format width for aligned output: Real programs rarely print raw numbers without worrying about alignment. The %Nd specifier (where N is the field width) is the fundamental tool for producing tabular output in C.
  • Nested loops produce 2D output: The outer loop produces rows; the inner loop produces columns within each row. This two-dimensional thinking is the same pattern used for matrix operations, pattern programs, and any output where both row and column matter.

Frequently Asked Questions

How do you print tables for multiple numbers at once?

Wrap the single-table loop in an outer loop: for (n = start; n <= end; n++). For example, to print tables 2 through 20, set start=2 and end=20. Print a blank line between tables for readability. This is the full N×N grid pattern, just with a header for each row.

How do you print a multiplication table up to 20 instead of 10?

Change the upper bound: for (i = 1; i <= 20; i++). Widen the format specifiers accordingly — the products reach up to n×20, so if n=20 the largest value is 400 (3 digits). Use %5d for the product column to keep alignment.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Test your understanding: C Aptitude Questions — or try our C Programming Quiz App on Android.

3 comments on “C Program to Print Multiplication Table – Single Table and N×N Grid

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>