Skip to content

Task 3: Mario

Create mario/mario.c. Ask the user for a positive integer height, then print a right-aligned pyramid made of # characters.

For a height of 4, the program should produce:

   #
  ##
 ###
####

Requirements

  • Read the height with scanf.
  • If the height is less than 1, ask again.
  • Print exactly the requested number of rows.
  • Right-align every row.
  • Use loops instead of writing each row manually.

You may assume that the user enters an integer.

Starter code

#include <stdio.h>

int main(void)
{
    int height;

    // Ask for a positive height.

    // Print each row of the pyramid.
}
Hints

For each row, think separately about:

  1. how many spaces appear before the first brick; and
  2. how many bricks appear after those spaces.

If rows are numbered from 1 through height, row r contains r bricks and height - r leading spaces.

Compile and test

cc mario.c -o mario
./mario

Test heights 1, 2, 4, and 8. Also enter 0 and a negative integer to confirm that the program asks again.

Continue with Task 4: Coins.