how to use factorial function inc code example
Example 1: factorial in c++
#include
#include
using namespace std;
int fact(int i){
if (i <= 1) return 1;
else return i*fact(i-1);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
cout << fact(N) << "\n";
return 0;
}
Example 2: Factorial Number
// METHOD ONE
const factorialNumber = num => {
let factorials = []
for(let i = 1; i <= num; i++) factorials.push(i)
return factorials.reduce((acc , curr) => acc * curr, 1)
}
// METHOD TWO
const factorialNumber = num => {
let factorial = 1, i = 1
while(i <= num){ factorial *= i; i++ }
return factorial
}
// METHOD THREE
function factorialNumber(num) {
if(num < 1) return 1
else return factorialNumber(num - 1) * num
}