Logo  

CS456 - Systems Programming

Displaying exercises/e5/files/pyramid.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  if (argc < 2) {
    printf("Usage: pyramid <height>\n");
    return 0;
  }
  int h = atoi(argv[1]);
  if (h <= 0) {
    printf("height must be positive.\n");
    return 1;
  }

  int s = h-1;
  int a = 1;
  int i, j;

  for(i = 0; i < h; i++) {
    for(j = 0; j < s; j++) putchar(' ');
    for(j = 0; j < a; j++) putchar('*');
    putchar('\n');
    s -= 1;
    a += 2;
  }
  
  return 0;
}