Skip to main content
1 of 3
Bryan
  • 21
  • 2

C

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

int main(int argc, char** argv)
{
    // iteratively compute factorial here

    long int n = 0, i = 0, r = 1 /* product starts at one */;

    if (argc > 1)
    {
        n = strtol(argv[1], NULL, 10);
        if (n < 0)
        {
            printf("arg must be >= 0\n");
            exit(-1);
        }
    }

    i = n;
    while (i)
    {
        result = result * i;
        i--;
    }

    printf("%ld factorial is %ld\n", n, result);
    // done
}

Pronounciation:

pound include standard
I/O dot h pound include
standard lib dot h

int main int arg c
comma char star star arg v
open brace comment

iteratively
compute factorial here
long int n equals

zero comma i
equals zero comma r
equals one comment

product starts at one
semicolon if arg c
is greater than one

open brace n is
str-to-l of arg v sub
one comma NULL comma ten

semicolon if
n less than zero open
brace printf arg must

be greater than or
equal to zero backslash
n semicolon

exit negative
one semicolon end brace
end brace i equals

n semicolon
while i open brace r
equals r times i

semicolon i
decrement semicolon
close brace comment print

the result printf
percent d factorial
equals percent d

whack n comma n
comma r semicolon
comment done end brace

Bryan
  • 21
  • 2