Created
October 22, 2018 21:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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