-
Notifications
You must be signed in to change notification settings - Fork 0
/
theTriangle.c
45 lines (37 loc) · 1.06 KB
/
theTriangle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// C program that uses the * character to draw a triangle of a given number of rows.
//
#include <stdio.h>
void drawTriangle(rows){
int rowCounter, colCounter;
// Loop for the entire triangle being printed
for(rowCounter = 1; rowCounter <= rows; rowCounter++){
//Prints * for the triangle, not including the last row
if(rowCounter < rows){
for(colCounter = 1; colCounter <= (2 * rows - 1); colCounter++){
//Prints left side of triangle Prints right side of triangle
if(colCounter == (rows - rowCounter + 1) || colCounter == (2 * rows - 1 - (rows - rowCounter))){
printf("*");
} else if (colCounter == 2 * rows - 1){
//Goes to new line of triangle
printf(" \n");
} else {
printf(" ");
}
}
// Prints the last row of *
} else if(rowCounter == rows){
for(colCounter = 1; colCounter <= (2 * rows - 1); colCounter++){
printf("*");
}
}
}
printf("\n");
}
int main(void){
int numRows;
printf("Enter the number of rows in the triangle: ");
scanf("%d", &numRows);
drawTriangle(numRows);
return 0;
}