-
Notifications
You must be signed in to change notification settings - Fork 0
/
practical5.cpp
38 lines (31 loc) · 1002 Bytes
/
practical5.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
38
#include<bits/stdc++.h>
using namespace std;
void iterativeFactorial(int num, int factorial = 1)
{
for(int i = 2 ; i <= num ; i++)
factorial *= i;
cout << "\nFactorial (iterartive) : " << factorial;
}
void recursiveFactorial(int num , int factorial = 1)
{
if(num == 1)
{
cout << "\n\nFactorial (recursive) : " << factorial;
return;
}
recursiveFactorial(num - 1 , factorial * num);
}
int main()
{
int num;
cout << "Enter number to find factorial : ";
cin >> num;
auto start = chrono::steady_clock::now();
iterativeFactorial(num);
auto end = chrono::steady_clock::now();
cout << "\nTime analysis of iterative : " << chrono::duration_cast<chrono::nanoseconds>(end-start).count() << "ns";
start = chrono::steady_clock::now();
recursiveFactorial(num);
end = chrono::steady_clock::now();
cout << "\nTime analysis of recursive : " << chrono::duration_cast<chrono::nanoseconds>(end-start).count() << "ns";
}