C bootcamp · Lab 29

Star triangle

easyNested Loops10 minLesson: Nested Loops

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 a right triangle of asterisks with n rows.

Row i has i stars and no extra spaces. Nested loops: outer rows, inner stars.

Input. One integer n ≥ 1.

Output. n lines of *, **, ***, …

Constraints

  • 1 ≤ n ≤ 20

Examples

Example 1
Input
3
Output
*
**
***
Hint
  1. for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) putchar('*'); putchar('\n'); }
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) return 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < i; j++) putchar('*');
    putchar('\n');
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.