Skip to content
Devtoolry

Patterns and small projects · beginner

C Program to Print the Multiplication Table of a Number

Print the multiplication table of a number in C with a for loop and printf field widths for aligned columns, reading the number with scanf().

A multiplication table is one loop, and a good place to learn printf field widths: %3d prints a number right-aligned in three characters, so the columns stay straight no matter how many digits each product has.

The program reads the number from the stdin box, so put a number there before you run it.

The program

C
#include <stdio.h>

int main(void) {
    int n;
    if (scanf("%d", &n) != 1) {
        printf("Please enter a number.\n");
        return 1;
    }
    for (int i = 1; i <= 10; i++)
        printf("%d x %2d = %3d\n", n, i, n * i);
    return 0;
}
Input (stdin)
7
Output
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

How it works

  1. scanf reads the number (7 here) and the program stops with a message if it is not a number.
  2. The loop runs i from 1 to 10.
  3. %2d and %3d pad the multiplier and the product with spaces on the left, so every row has the same width.

Another way: Full table with nested loops

Two nested loops print a whole grid; %4d gives each cell four characters.

C
#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 5; row++) {
        for (int col = 1; col <= 5; col++)
            printf("%4d", row * col);
        printf("\n");
    }
    return 0;
}
Output
   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25

Common mistakes

  • Writing scanf("%d", n) without &, which passes the value instead of the address.
  • Using i < 10, which stops at 9.
  • Using %d without a width, so the columns shift when the product gets another digit.

Try it yourself

  • Read an upper limit as a second number and print up to that.
  • Print a 12 × 12 grid with a header row and column.

Open the program in the C Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 C examples, or write your own program in the online C Compiler.