Medium ยท nested loops

Star Pyramid

Row i of n: n โˆ’ i spaces, then i stars. Nested loops where the leading spaces are load-bearing โ€” the judge compares them too.

Problem

Input: one integer n (1 โ‰ค n โ‰ค 20).

Output: n rows; row i (1-based) is n โˆ’ i spaces followed by i asterisks.

Sample (n = 3):

  *
 **
***

Your solution

Program.cs โ€” your solution
Custom input for "Run my input"

Judging runs entirely in your browser against hidden tests. Your code is saved locally as you type. Ctrl+Enter judges.

Stuck? Hints

Hint 1 โ€” three loops per row

Outer loop: rows 1..n. Inside: print spaces (n - row of them), then stars (row of them), then one Console.WriteLine().

Hint 2 โ€” the classic failure

Spaces with Console.Write(" "), not WriteLine. And the newline goes after BOTH inner loops, inside the outer one only.

Solution (try first!)
int n = int.Parse(Console.ReadLine());
for (int row = 1; row <= n; row++)
{
    for (int s = 0; s < n - row; s++) Console.Write(" ");
    for (int k = 0; k < row; k++) Console.Write("*");
    Console.WriteLine();
}

Exact-output problems are their own discipline โ€” the nested-loop lesson walks this exact shape. Next: GCD.