Read the question, write C++ on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read n and print its times table from 1 to 10.
Each line reads n x i = result with a single space around each symbol.
Input. One line: an integer n.
Output. Ten lines, e.g. 3 x 1 = 3 up to 3 x 10 = 30.
Constraints
- Loop condition i <= 10 (not i < 10) to include ten.
Examples
Input
3
Output
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
Hint
- for (int i = 1; i <= 10; i++) { ... }
- Stream the pieces: std::cout << n << " x " << i << " = " << n * i << "\n";
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <iostream>
int main() {
int n;
std::cin >> n;
for (int i = 1; i <= 10; i++) {
std::cout << n << " x " << i << " = " << n * i << "\n";
}
return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.