From 66e321063262f57aa4130abea30db440eaefc131 Mon Sep 17 00:00:00 2001 From: gaonkarsahil Date: Sun, 24 Oct 2021 15:58:47 +0530 Subject: [PATCH] Program to compute factorial of a number using recursion. --- Code/C/FactorialRecursion.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Code/C/FactorialRecursion.c 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