Skip to content
C++ Better Explained
Go back
Multiplication Table Program in C++ (Using Loops)

Multiplication Table Program in C++

Printing a multiplication table is one of the best first programs you can write, because it teaches loops in the most visual way possible. We’ll start with a single table, add user input, make it line up neatly, then build the full grid.


The Simplest Version: One Number’s Table

A multiplication table for 7 is just 7 x 1, 7 x 2, and so on up to 7 x 10. A single for loop handles it perfectly — the loop counter becomes the number we multiply by:

#include <iostream>

int main() {
    int n = 7;
    for (int i = 1; i <= 10; ++i) {
        std::cout << n << " x " << i << " = " << n * i << "\n";
    }
    return 0;
}

Output:

7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70

Each time through the loop, i climbs by one, and n * i gives the next line. That’s the whole idea.


Letting the User Pick the Number

A hard-coded 7 is boring. Let’s ask the user which table they want with std::cin:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter a number: ";
    std::cin >> n;

    for (int i = 1; i <= 10; ++i) {
        std::cout << n << " x " << i << " = " << n * i << "\n";
    }
    return 0;
}

Now the program works for any number the user types. This tiny change — reading input instead of fixing the value — is what turns a snippet into a real, reusable program.


Making the Output Line Up

Once numbers get into the hundreds, the columns look ragged. The <iomanip> header gives us std::setw, which pads each value to a fixed width so everything aligns:

#include <iostream>
#include <iomanip>

int main() {
    int n = 12;
    for (int i = 1; i <= 10; ++i) {
        std::cout << std::setw(2) << n << " x "
                  << std::setw(2) << i << " = "
                  << std::setw(3) << n * i << "\n";
    }
    return 0;
}

std::setw(3) reserves three character slots for the result, so 24 and 120 still end in the same column. Neat output makes your programs feel polished.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The Full Grid: A Times Table

To print the classic 10x10 times table, you need a loop inside a loop. The outer loop picks the row; the inner loop walks across the columns:

#include <iostream>
#include <iomanip>

int main() {
    for (int row = 1; row <= 10; ++row) {
        for (int col = 1; col <= 10; ++col) {
            std::cout << std::setw(4) << row * col;
        }
        std::cout << "\n";   // move to the next line after each row
    }
    return 0;
}

The inner loop runs completely (all 10 columns) for each step of the outer loop, producing a tidy grid. Notice the "\n" sits outside the inner loop — that’s what ends each row. This nested-loop pattern is the foundation for grids, boards, and tables of every kind.


Recap: What Each Piece Does

Master this and you’ve genuinely understood loops — the workhorse of every C++ program.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
Integer Division in C++: Why 5 / 2 Equals 2 (Not 2.5)
Next Post
The sizeof Operator in C++: Find the Size of Any Type

Keep Learning