Possible Solution

Content

Some remarks:

Function factorial with for-loop

unsigned long n = 5;

unsigned long
factorial(unsigned long n)
{
    unsigned long result = 1;
    for (unsigned long i=2; i<=n; ++i) {
        result *= i;
    }
    return result;
}

int
main()
{
    return factorial(n);
}
thales$ gcc -Wall -std=c11 -o factorial_for factorial_for.c
thales$ ./factorial_for
thales$ echo $?
120
thales$ 

Function factorial with while-loop

unsigned long n = 5;

unsigned long
factorial(unsigned long n)
{
    unsigned long result = 1;
    while (n>1) {
        result *= n;
        --n;
    }
    return result;
}

int
main()
{
    return factorial(n);
}
thales$ gcc -Wall -std=c11 -o factorial_while factorial_while.c
thales$ ./factorial_while
thales$ echo $?
120
thales$ 

Function factorial with recursive calls

unsigned long n = 5;

unsigned long
factorial(unsigned long n)
{
    return n>1 ? n*factorial(n-1) : 1;
}

int
main()
{
    return factorial(n);
}
thales$ gcc -Wall -std=c11 -o factorial_rec factorial_rec.c
thales$ ./factorial_for
thales$ echo $?
120
thales$ 

Note that this function uses the conditional expression cond ? value_if_true : value_if_false. You also can implement this with an if-statement:

unsigned long
factorial(unsigned long n)
{
    // return n>1 ? n*factorial(n-1) : 1;
    if (n>1) {
        return n*factorial(n-1);
    } else {
        return 1;
    }
}