Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- int i, j, num_lines;
- int middle_spaces = 0;
- //gets user input
- printf("Please enter an integer: ");
- scanf("%d", &num_lines);
- //this loop prints most of the triangle except the last line
- for (i = (num_lines-1); i > 0; i--)
- {
- //prints the empty spaces before the first * on every line
- for (j = i; j > 0; j--)
- {
- printf(" ");
- }
- //prints the first star
- printf("*");
- //if its the top row, print new line and go to next row
- if(middle_spaces == 0)
- {
- printf("\n");
- middle_spaces = 1;
- continue;
- }
- //prints the spaces inside the triangle
- for (j = middle_spaces; j > 0; j--)
- {
- printf(" ");
- }
- //prints the 2nd "*"
- printf("*\n");
- middle_spaces +=2;
- }
- //prints the last line of the triangle
- for (i = ((num_lines*2) - 1); i > 0; i--)
- {
- printf("*");
- }
- printf("\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment