Tuesday, February 5, 2019

Factorial using function

Write a program in C to find the factorial of a number using function.

Code:

#include <stdio.h>

long factorial(long x);

int main()
{
    int num;
    long fact;
    printf("Enter an integer: ");
    scanf("%d", &num);
    fact = factorial(num);
    printf("The factorial of %d is %ld", num, fact);

    return 0;
}


long factorial(long x){
    if(x == 0) return 1;
    return x * factorial(x - 1);
}


No comments: