/* quick demo of recursive factorial and non-recursive factorial */
/* P. Conrad   CISC105  Fall 2004  */


#include <stdio.h>


int fact(int n)
{
  int total = 1;
  int i;

  for (i=2; i<=n; i++)
    total *= i;

  return total;

}

int factA(int n)
{
  int product = 1;

  if (n<0)
    return -1;

  while (n>1)
    {
      product *= n;
      n--;
    }
  return product;

}


int main(int argc, char *argv[])
{
  
  int n;

  int result;

  if (argc!=2)
    {
      printf("Usage: %s number\n",argv[0]);
      printf("  number should be an integer >= 0\n");
      exit(-1);
    }

  n = atoi(argv[1]); /* convert ascii to integer */

  if (n<0)
    {
      printf("Sorry, number should be >=0 \n");
      printf("You entered %s\n", argv[1]);
      printf("(This is better than printing: You entered %d)\n", n);
      printf("Please run the program again.\n");
      exit(-1);
    }
  
  result = factA(n);

  printf("result=%d\n",result);

  return 0;
}


