Print a multiplication table
Description: Print 7 x 3 = 21 for 1 to 10.
Hint:
- A for loop and a format with a width, like
{:2}.
Solution
#include <print>
int main() {
int n = 7;
for (int i = 1; i <= 10; i++) {
std::println("{} x {:2} = {:2}", n, i, n * i);
}
}
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
{:2}uses at least 2 places, so the numbers line up.
Read C++ for Loop.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |