0. Factorial of a number
Given a positive integer, N. Find the factorial of N.
Example 1:
Input:
N = 5
Output:
120
Explanation:
5*4*3*2*1 = 120
Example 2:
Input:
N = 4
Output:
24
Explanation:
4*3*2*1 = 24
, #include <iostream>
using namespace std;
// this function fac() will return us the factorial of the number n
int fac(int n)
{
// base condition
if (n == 0)
return 1;// recursion call
return n * fac(n - 1);
}
int main()
{
// Write your code here
int n;
cin >> n;
if (n < 0)
cout << "Error";
else
{
cout << fac(n);
}
return 0;
}
/*
time complexity : O(n)
space complexity : O(n)
*/
Given a positive integer, N. Find the factorial of N.
Example 1:
Input:
N = 5
Output:
120
Explanation:
5*4*3*2*1 = 120
Example 2:
Input:
N = 4
Output:
24
Explanation:
4*3*2*1 = 24
, #include <iostream>
using namespace std;
// this function fac() will return us the factorial of the number n
int fac(int n)
{
// base condition
if (n == 0)
return 1;// recursion call
return n * fac(n - 1);
}
int main()
{
// Write your code here
int n;
cin >> n;
if (n < 0)
cout << "Error";
else
{
cout << fac(n);
}
return 0;
}
/*
time complexity : O(n)
space complexity : O(n)
*/