#include <stdio.h> void center(char ch, int count, int length) { int blanks = length-count; int j; for ( j=0; j<blanks/2; j++ ) putchar( ' ' ); for ( j=0; j<count; j++ ) putchar( ch ); putchar( '\n'); } void starBlock( int r, int c, int lineSize ) { int j; for ( j=0; j<r; j++ ) center( '*', c, lineSize ); } void pyramid( int blocks ) { int b=0; int lineSize = 2*blocks-1; for ( b=1; b<=blocks; b++ ) starBlock( b, 2*b-1, lineSize ); } int main(int argc, char *argv[]) { pyramid( 5 ); system("PAUSE"); return 0; }
Comments: The base of the pyramid is (2*blocks-1) characters wide. Use that as the linesize for all lines of the picture.