Guest User

Untitled

a guest
Feb 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int i, j, num_lines;
  6. int middle_spaces = 0;
  7.  
  8. //gets user input
  9. printf("Please enter an integer: ");
  10. scanf("%d", &num_lines);
  11.  
  12. //this loop prints most of the triangle except the last line
  13. for (i = (num_lines-1); i > 0; i--)
  14. {
  15. //prints the empty spaces before the first * on every line
  16. for (j = i; j > 0; j--)
  17. {
  18. printf(" ");
  19. }
  20.  
  21. //prints the first star
  22. printf("*");
  23.  
  24. //if its the top row, print new line and go to next row
  25. if(middle_spaces == 0)
  26. {
  27. printf("\n");
  28. middle_spaces = 1;
  29. continue;
  30. }
  31.  
  32. //prints the spaces inside the triangle
  33. for (j = middle_spaces; j > 0; j--)
  34. {
  35. printf(" ");
  36. }
  37.  
  38. //prints the 2nd "*"
  39. printf("*\n");
  40. middle_spaces +=2;
  41. }
  42.  
  43. //prints the last line of the triangle
  44. for (i = ((num_lines*2) - 1); i > 0; i--)
  45. {
  46. printf("*");
  47. }
  48.  
  49. printf("\n");
  50.  
  51. return 0;
  52. }
Add Comment
Please, Sign In to add comment