C bootcamp · Lab 06

Times table

easyFor Loop10 minLesson: For Loop

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a positive integer n. Print the multiplication table from n × 1 through n × 10.

Each line must look like: n x i = product

Use a space on each side of x and =. Ten lines, no extra blank lines.

Input. One integer n.

Output. Ten lines in the form n x i = product

Constraints

  • 1 ≤ n ≤ 20

Examples

Example 1
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
  1. A for loop from i = 1 to i <= 10 is the whole program after you read n.
  2. printf("%d x %d = %d\n", n, i, n * i); matches the required spacing.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int n;
  if (scanf("%d", &n) == 1) {
    for (int i = 1; i <= 10; i++) {
      printf("%d x %d = %d\n", n, i, n * i);
    }
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.