Use function recursion to calculate factorial of a number. - C Function

C examples for Function:Recursive Function

Description

Use function recursion to calculate factorial of a number.

Demo Code

#include <stdio.h>

unsigned int f, x;

unsigned int factorial(unsigned int a){
    if (a == 1)/*w w w . j a  v a 2 s .c o  m*/
        return 1;
    else{
        a *= factorial(a-1);
        return a;
    }
}


int main( void ){
    puts("Enter an integer value between 1 and 8: ");
    scanf("%d", &x);

    if( x > 8 || x < 1){
        printf("Only values from 1 to 8 are acceptable!");
    }else{
        f = factorial(x);
        printf("%u factorial equals %u\n", x, f);
    }

    return 0;
}

Related Tutorials