Here is how I solved the version of this problem with both sides, but you can make it work with only one, just use 2 for-loops instead of three.
If we have to take a number 1 – 8 as an input which is the height of our pyramid, we can use one for loop to print out each row of the pyramid.
Inside this loop, we will need another two for loops that print spaces and hashes.
And since we need the other side of the pyramid as well, we will use three loops in total within our main loop.
Why three loops and not four? Because we don’t actually need to print space on the right side of the pyramid.
Beside the height variable we need another one that will work as a counter, since we cannot manipulate our height variable.
These for loops work in opposite direction, i.e. the more spaces we have, less hashes we print and vice versa.
So, the final CS50 Pset 1 Mario More solution looks exactly like this:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do {
height = get_int("Height: ");
}
while (height < 1 || height > 8);
if (height > 0 || height < 9) {
int counter = 0;
for (int row=0; row<height; row++) {
if (counter != height) {
for (int spaces = (height-1) - counter; spaces > 0; spaces--) {
printf(" ");
}
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//ignore this block below if you want only one side
printf(" ");
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//##################################################
printf("\n");
counter++;
}
}
}
}
I actually made a blog post about this since I like to keep notes in case that you need more information.