Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- int main(void)
- {
- int h; //The height asked from the user. The limits the height of the pyramid
- int i; //First counter that is used for the rows
- int j; //Counter
- do
- {
- h = get_int("height: ");
- }
- while (h < 1 || h > 8);
- // This do while loop asks the user for a height, and if it does not fall within the given parameters it will simply ask again until it gets
- // an answer that does.
- for (i = 0; i < h; i++)
- { // This counter only changes for each row
- for (j = 0; j < h - 1 - i ; j++){ // counter starts at 0. Checks if j < max height - 1 - i , if no, prints a space then adds 1 to j.
- printf(" "); // Keeps printing spaces until j = max height - 1 - i, which will go from h-1 to 0
- }
- for (j = 0; j <= i ; j++){ // counter starts at 0. Checks if k is less or equal to i, if no, prints a hash then adds 1 to k.
- printf("#"); // Keeps printing spaces until there are as many hashes as the row number
- }
- for (j= 0; j < 2 ; j++){ // Adds 2 spaces in each row
- printf(" ");
- }
- for(j = 0; j <= i ; j++){ // Same as before but then puts hashes in other orientation
- printf("#");
- }
- printf("\n"); //Goes to next line to separate the rows
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement