From 522c527d0b9cac1b0c4be2de6669c3827591fd40 Mon Sep 17 00:00:00 2001 From: shivaabhishek07 Date: Sat, 1 Oct 2022 00:07:51 +0530 Subject: [PATCH] Added new algorithm in C/recursion --- algorithms/C/recursion/factorial-of-number.c | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 algorithms/C/recursion/factorial-of-number.c 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); +}