-
Notifications
You must be signed in to change notification settings - Fork 617
/
factorial_using_recursion.cpp
38 lines (28 loc) · 1.27 KB
/
factorial_using_recursion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* In this Problem, our objective was to find the factorial of a number n entered by the user.
Factorial of a number n is n! = n*(n-1)*(n-2)*........*1
Using recursion we can write factorial of n as n! = n*(n-1)!
We used n = 0 as our base case (0! = 1), i.e.
if(n == 0) : return 1;
*/
#include<iostream> // header file required for Input/Output in the program
using namespace std;
int factorial(int n) // Function to calculate this factorial
{ if(n == 0) // Base Case if n = 0
return 1; // 0! = 1
return n*factorial(n-1); // Multiplied the current number and recursed the factorial (n-1)
}
int main() // main function from where the execution of code starts
{ int n; // number for which we need to find factorial
cin >> n; // number input by the user
cout << factorial(n) <<endl; // calling the factorial function created
return 0;
}
/* Test Case :
Input : 5
Output : 120
Input : 5
Output : 120
*/
/* Time Complexity in this case is : O(N)
Space Complexity in this case is : O(1)
*/