Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 37 lines (32 sloc) 0.718 kb
e8b904e @cooljeanius make separate library for factorial function
authored
1 /*
2 * libfactorial.c
3 * Pascals_Triangle
4 *
5 * Created by Eric Gallager on 6/30/12.
6 * Copyright 2012 George Washington University. All rights reserved.
7 *
8 */
9
10 #include "libfactorial.h"
11
12 /*
13 * Computes n!
14 * n must be greater than or equal to zero -- premature exit on negative data
15 */
16 int factorial(int n); // prototype
17 int
18 factorial(int n) {
19 int i = 0; /* local variables */
20 int product = 1;
21 if (n < 0) {
22 printf("\n***Function factorial reports ");
23 printf("ERROR: %d! is undefined***\n", n);
24 exit(1);
25 } else {
26 /* Compute the product n x (n-1) x (n-2) x ... x 2 x 1 */
27 for (i = n; i > 1; --i) {
28 product = product * i;
29 }
30 /* Return function result */
31 return (product);
32 }
33 }
34
35 // newline \n
36
Something went wrong with that request. Please try again.