#include <stdio.h> #include <stdlib.h> /* Puzzle A20 - print a triangle of stars, n rows tall */ int main(int argc, char *argv[]) { int row, j, spaces, stars; int n=12; for ( row=0; row < n; row++ ) { stars = 2*row+1; spaces = (2*n-1-stars)/2; for ( j=0; j < spaces; j++ ) printf("."); for ( j=0; j < stars; j++ ) printf("*"); for ( j=0; j < spaces; j++ ) printf("."); printf("\n"); } printf("\n"); system("PAUSE"); return 0; }
Comments:
Each row of the picture has three parts: the first line of dots,
the line of stars, the last line of dots.
The three inside for
-loops
deal with each of these parts.
Before they start,
the number of characters each inside loop must output is calculated.