******
*****
****
***
**
*
The first thing we must do is ask and get from the user the number of rows he/she wants. So the following
code belongs at the beginning of main:
int numRows;
printf("Please enter the number of rows you want.\n");
scanf("%d", &num);
OBSERVATION: So from left to right, the third line is some spaces followed by some asterisks followed by a newline which sets up the start of the next line.
repeat: print ' ' repeat: print '*' print '\n'Code like this is fake code. It is not the C language; it just helps us with the planning. The offical name for this is pseudo-code.
repeat numSpc copies: print ' ' repeat numAst copies: print '*' print '\n' //prepare for the next row. numSpc++ numAst--The last two lines are because each row has one more space than the row before it and one fewer asterisk.
repeat numRows copies:
repeat numSpc copies:
print ' '
repeat numAst copies:
print '*'
print '\n'
//prepare for the next row.
numSpc++
numAst--
int numSpc = 0;
int numAst = numRows;
repeat numRows copies:
repeat numSpc copies:
print ' '
repeat numAst copies:
print '*'
print '\n'
//prepare for the next row.
numSpc++
numAst--
int numSpc = 0;
int numAst = numRows;
int i=0;
while (i<numRows) {
int j=0; //new row; no spaces printed yet
while (j<numSpc) {
printf(" ");
j++; //one more space printed in current row
}
int k=0; //no asterisks printed yet
while (k<numAst) {
printf("*");
k++; //one more asterisk printed in current row
}
printf("\n");
//prepare for the next row.
numSpc++;
numAst--;
//one more row has been printed:
i++;
}
This code should be put into main() { } after the scanf.
int numSpc = 0;
int numAst = numRows;
int i, j, k;
for(i=0; i<numRows; i++) {
for(j=0; j<numSpc; j++)
printf(" ");
for(k=0; k<numAst; k++)
printf("*");
printf("\n");
//prepare for the next row.
numSpc++;
numAst--;
}