Asterisk triangles are boring. Give your teacher something with a little more pizzaz!
Code:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
char buff[BUFSIZ];
int height;
int spaces;
int i, j;
printf ( "Enter the height (32 is a good one): " );
fflush ( stdout );
if ( fgets ( buff, sizeof buff, stdin ) == NULL ) {
fprintf ( stderr, "Error reading input\n" );
return EXIT_FAILURE;
}
if ( sscanf ( buff, "%d", &height ) != 1 ) {
fprintf ( stderr, "Invalid input detected\n" );
return EXIT_FAILURE;
}
spaces = height / 2;
for ( i = 0; i < height; i += 2, spaces-- ) {
for ( j = 0; j < spaces; j++ )
putchar ( ' ' );
for ( j = 0; j <= i; j++ )
putchar ( ( ~i & j ) ? ' ' : '*' );
putchar ( '\n' );
}
return EXIT_SUCCESS;
}