Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6. int h; //The height asked from the user. The limits the height of the pyramid
  7. int i; //First counter that is used for the rows
  8. int j; //Counter
  9.  
  10.  
  11. do
  12. {
  13. h = get_int("height: ");
  14. }
  15. while (h < 1 || h > 8);
  16.  
  17. // 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
  18. // an answer that does.
  19.  
  20. for (i = 0; i < h; i++)
  21. { // This counter only changes for each row
  22.  
  23. 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.
  24. printf(" "); // Keeps printing spaces until j = max height - 1 - i, which will go from h-1 to 0
  25. }
  26.  
  27. 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.
  28. printf("#"); // Keeps printing spaces until there are as many hashes as the row number
  29. }
  30.  
  31. for (j= 0; j < 2 ; j++){ // Adds 2 spaces in each row
  32. printf(" ");
  33. }
  34.  
  35. for(j = 0; j <= i ; j++){ // Same as before but then puts hashes in other orientation
  36. printf("#");
  37. }
  38.  
  39. printf("\n"); //Goes to next line to separate the rows
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement