C++ Program to Print Multiplication Table

C++ Program to Print Multiplication Table

Learning C++ is exciting, especially when you start creating programs that do useful tasks. One of the first programs beginners often try is printing the multiplication table of a number. This simple program helps you understand loops, input/output operations, and basic arithmetic in C++. Multiplication tables are everywhere—from calculators to educational software—and learning how to code them gives you a strong foundation for more complex projects.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Multiplication Table Using a for Loop

This program prints the multiplication table of a number entered by the user using a simple for loop. It’s the most straightforward approach, perfect for beginners to understand the basics of iteration in C++.

#include <iostream>

using namespace std;

int main() {

    int number;

    cout << "Enter a number: " << endl;
    cin >> number;

    cout << "Multiplication table of " << number << " is:" << endl;

    for(int i = 1; i <= 10; i++) {
        cout << number << " x " << i << " = " << number * i << endl;
    }

    return 0;

}

This program uses a for loop to multiply the entered number from 1 to 10. Beginners can see how loops simplify repetitive tasks, making it easy to print the whole multiplication table without writing many cout statements.

Program 2: Multiplication Table Using a while Loop

This version uses a while loop, showing another way to achieve the same goal. while loops are useful when the number of repetitions may not be known beforehand.

#include <iostream>

using namespace std;

int main() {

    int number, i = 1;

    cout << "Enter a number: " << endl;
    cin >> number;

    cout << "Multiplication table of " << number << " is:" << endl;

    while(i <= 10) {

        cout << number << " x " << i << " = " << number * i << endl;
        i++;

    }

    return 0;

}

Here, the loop continues as long as i is less than or equal to 10. Each iteration prints a result and increases i. Beginners can understand how while loops control repetition based on a condition.

Program 3: Multiplication Table Using a do-while Loop

A do-while loop ensures the multiplication table prints at least once, even if the condition is false initially. This helps beginners understand differences between loop types.

#include <iostream>

using namespace std;

int main() {

    int number, i = 1;

    cout << "Enter a number: " << endl;
    cin >> number;

    cout << "Multiplication table of " << number << " is:" << endl;

    do {

        cout << number << " x " << i << " = " << number * i << endl;
        i++;

    } while(i <= 10);

    return 0;

}

The do-while loop first executes the code block and then checks the condition. Beginners learn that this guarantees at least one execution of the loop body.

Program 4: Colorful Multiplication Table in Console

This program adds colors to the multiplication table using ANSI escape codes. It makes your console output visually appealing and easier to read.

#include <iostream>

using namespace std;

int main() {

    int number;

    cout << "Enter a number: " << endl;
    cin >> number;

    cout << "\033[1;34mMultiplication table of " << number << ":\033[0m" << endl;

    for(int i = 1; i <= 10; i++) {

        cout << "\033[1;32m" << number << " x " << i << " = " 
             << number * i << "\033[0m" << endl;

    }

    return 0;

}

Here, the heading is displayed in blue and results in green. Beginners see how to make output more readable and visually engaging while still practicing loops.

Program 5: Full 12×12 Multiplication Table

This program prints a full 12×12 multiplication table in a clean, readable grid format.

#include <iostream>

using namespace std;

int main() {

    cout << "12x12 Multiplication Table:" << endl;

    for(int i = 1; i <= 12; i++) {

        for(int j = 1; j <= 12; j++) {
            cout.width(4);
            cout << i * j;
        }

        cout << endl;
    }

    return 0;

}

Nested loops handle rows and columns, and cout.width(4) ensures numbers align neatly. Beginners learn nested loops, which are essential for grids, matrices, and other structured output.

Program 6: Colorful 12×12 Multiplication Table

For extra fun, this program prints a 12×12 table with alternating row colors using ANSI escape codes. It makes the table easy to read and visually appealing.

#include <iostream>

using namespace std;

int main() {

    cout << "\033[1;34m12x12 Multiplication Table:\033[0m" << endl;

    for(int i = 1; i <= 12; i++) {

        for(int j = 1; j <= 12; j++) {

            if(i % 2 == 0)
                cout << "\033[1;32m"; // green for even rows

            else
                cout << "\033[1;33m"; // yellow for odd rows

            cout.width(4);
            cout << i * j << "\033[0m";

        }

        cout << endl;

    }

    return 0;

}

Alternating row colors make large tables easier to follow. Beginners get experience combining nested loops with formatting and colors.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask when learning to print multiplication tables in C++.

Q1: What is the easiest loop for beginners to use?
A1: A for loop is usually easiest because you know exactly how many times it will run. It is predictable and straightforward for printing multiplication tables.

Q2: Can I print a multiplication table for numbers greater than 10?
A2: Yes! You can change the loop’s end value to any number depending on how many rows you want.

Q3: Why do we use cin and cout?
A3: cin gets input from the user, and cout displays output. Both are part of the C++ standard library.

Q4: How do I align numbers neatly in a table?
A4: You can use cout.width() to set a fixed width for each number, which helps keep columns aligned.

Q5: Can I add colors to my console output?
A5: Yes! Using ANSI escape codes, you can change text color and style to make output more readable and engaging.

Conclusion

Printing multiplication tables in C++ is a great way for beginners to practice loops, input/output, and basic arithmetic. By exploring for, while, do-while, and nested loops, you can print simple or full 12×12 tables. Adding colors makes learning fun and output visually appealing. Keep experimenting, try different formats, and soon you’ll be ready to tackle bigger projects confidently.

Additional & References

Practicing small programs like multiplication tables builds a solid foundation in C++. Once comfortable, you can explore loops, formatting, and more complex programs.

Scroll to Top