Added new algorithm in C/recursion

pull/860/head
shivaabhishek07 2022-10-01 00:07:51 +05:30
parent b67f5e1786
commit 522c527d0b
1 changed files with 27 additions and 0 deletions

View File

@ -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<stdio.h>
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);
}