Multiplication Table in C++

Print a multiplication table in C++ with a for loop. Use <= 10, not < 10.

BeginnerLoop ProgramsExample 1 of 14
multiplication-table.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num;
6
7 cout << "Enter a number: ";
8 cin >> num;
9
10 cout << "Multiplication table of " << num << ":" << endl;
11 for (int i = 1; i <= 10; i++) {
12 cout << num << " x " << i << " = " << (num * i) << endl;
13 }
14
15 return 0;
16}

Output

Enter a number: 5
Multiplication table of 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

What's going on

This C++ program prints a multiplication table for a number from 1 to 10.

int num;

cin >> num;

Sample: 5.

cout << "Multiplication table of " << num << ":" << endl;

Header line, then the rows.

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

<= 10, not < 10. < 10 prints 1 through 9 and looks almost right.

cout << num << " x " << i << " = " << (num * i) << endl;

Sample first and last rows: 5 x 1 = 5, 5 x 10 = 50.

Letters in cin leave num junk and you get a table of garbage. Check the stream if you're being nice.

Want 1 to 12? Change 10 in the loop. The rest stays.