diff --git a/algorithms/C/recursion/factorial-of-number.c b/algorithms/C/recursion/factorial-of-number.c new file mode 100644 index 00000000..55f1d98f --- /dev/null +++ b/algorithms/C/recursion/factorial-of-number.c @@ -0,0 +1,27 @@ +/* +Factorial of the number: Factorial of a non-negative integer is the multiplication of all positive integers smaller than or equal to n. +For example factorial of 6 is 6*5*4*3*2*1 which is 720. + Input | Output + 5 | 120 + 6 | 720 + 3 | 6 +Time Complexity - O(log(n)) +Space Complexity- O(1) + +*/ +#include +int fact(int n){ + if(n==1){ + return 1; + } + else{ + return n*fact(n-1); + } +} +void main(){ + int an; + printf("Enter a number"); + scanf("%d",&an); + int f=fact(an); + printf("factorial of %d is %d",an,f); +}