Advertisement
lukecontreras50

Untitled

Sep 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <cs50.h>
  4.  
  5.  
  6. //this functions prompts the user for height and returns the value
  7. int getHeight()
  8. {
  9.     int height = 0;
  10.  
  11. //k is functioning as a boolean to check if there will be valid input or not
  12.     int k = 0;
  13.  
  14. //keeps looping until user enters valid input
  15.     while(k==0)
  16.     {
  17.         height = get_int("Enter the height (no greater than 23)\n");
  18.         if(height>23||height<0)
  19.         {
  20.             k=0;
  21.         }
  22.         else
  23.         {
  24.             k=1;
  25.         }
  26.     }
  27.     return height;
  28. }
  29.  
  30.  
  31. //this function produces the half-pyramid
  32. int makePyramid()
  33. {
  34.     int height = getHeight();
  35.  
  36.     for(int i = 0; i<height; i++)
  37.     {
  38.  
  39.             int numSpaces = height - i;
  40.  
  41.  
  42.             for (int k = 0; k<numSpaces; k++)
  43.             {
  44.                 printf(" ");;
  45.             }
  46.  
  47.             for (int x = 0; x<i+1; x++)
  48.             {
  49.                 printf("#");
  50.             }
  51.  
  52.             printf("\n");
  53.  
  54.  
  55.     }
  56.  
  57.     return 0;
  58. }
  59.  
  60.  
  61. //makePyramid is called in the main
  62. int main(void)
  63. {
  64.     makePyramid();
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement