Skip to content

Instantly share code, notes, and snippets.

@gchristofferson
Created October 22, 2018 21:41
Show Gist options
  • Save gchristofferson/8f3ab74d889afe17ad09781bdc2a0acc to your computer and use it in GitHub Desktop.
Save gchristofferson/8f3ab74d889afe17ad09781bdc2a0acc to your computer and use it in GitHub Desktop.
C program from course work that takes number of rows as input and prints a pyramid with that number of rows.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int numRows;
do
{
// promt and validate user input
numRows = get_int("Pyramids' Number of Rows: ");
}
while (numRows < 0 || numRows > 23);
for (int i = 0; i < numRows; i++)
{
int numSpaces = numRows - (i + 1);
int numHashes = i + 1;
// print spaces for left pyramid
for (int j = 0; j < numSpaces; j++)
{
printf(" ");
}
// print hashes for left pyramid
for (int k = 0; k < numHashes; k++)
{
printf("#");
}
// print gap
printf(" ");
// print hashes for right pyramid
for (int l = 0; l < numHashes; l++)
{
printf("#");
}
// print new lines
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment