Next: power.c Up: Program Listings Previous: cio.c

factorial



\* e.g use of functions factorials *\
\* fact(n) = n*(n-1)*....2*1 *\

#include <stdio.h>

main()
{
    int n, m;

    printf("Enter a number: ");
    scanf("%d", &n);

    m = fact(n);
    printf("The factorial of %d is %d.\n", n, m);
    exit(0);
}

fact(n)
int n;
{
    if (n == 0)
        return(1);

    return(n * fact(n-1));
}


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994