Let’s recreate that pyramid in C, albeit in text, using hashes (#
) for bricks, a la the below. Each hash is a bit taller than it is wide, so the pyramid itself is also be taller than it is wide.
The program we’ll write will be called mario
. And let’s allow the user to decide just how tall the pyramid should be by first prompting them for a positive integer between, say, 1 and 8, inclusive.
Here’s how the program might work if the user inputs 8 when prompted:
./mario
Height: 8
#
##
###
####
#####
######
#######
########
Here’s how the program might work if the user inputs 4 when prompted:
$ ./mario
Height: 4
#
##
###
####
Here’s how the program might work if the user inputs 2 when prompted:
$ ./mario
Height: 2
#
##
And here’s how the program might work if the user inputs 1 when prompted:
$ ./mario
Height: 1
#
This is a simple challenge, but if you want to take on a more difficult one, try it on the website.
- First, create a new file, it called
mario.c
. - use
scanf
to get a integer. - Keep in mind that a hash is just a character like any other, so you can print it with
printf
. - You can actually “nest” loops, iterating with one variable (e.g.,
i
) in the “outer” loop and another (e.g.,j
) in the “inner” loop. For instance, here’s how you might print a square of height and widthn
, below. Of course, it’s not a square that you want to print!
- scanf
- printf
- for loop
- positive integer
The following content is for reference only. In order to have a better learning effect, please try to complete the exercises according to your own ideas.
reference
# include <stdio.h>
void main(){
int n;
printf("Height:");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i+1; j++)
{
printf("#");
}
printf("\n");
}
}