diff --git a/Code/C/FactorialRecursion.c b/Code/C/FactorialRecursion.c new file mode 100644 index 0000000..401a72f --- /dev/null +++ b/Code/C/FactorialRecursion.c @@ -0,0 +1,32 @@ +//Program to compute factorial of a number using recursion. +# include + +int fact(int); + +int main() +{ + int num, result; + printf("Enter a positive integer : \n"); + scanf("%d", &num); + + if(num < 0) + { + printf("Invalid positive integer. Please try again with positive integer."); + return 0; + } + + result = fact(num); + printf("The factorial of %d is %d.", num, result); + return 0; +} + +int fact(int n) +{ + if(n==0) + return 1; + + if(n==1) + return(n); + else + return(n*fact(n-1)); +} \ No newline at end of file