Last active
July 5, 2016 23:17
-
-
Save bhu1st/42ce14e7d6df06fa5495c8278dfb507f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. WAF to calculate Factorial | |
2. Calculate factorial Recursively | |
3. WAF factorial (number, true); | |
4. Improve/optimize the function | |
*/ | |
#include <iostream> | |
using namespace std; | |
int factorial(int NUM, bool type) { | |
int fact = 1; | |
if (NUM == 0) return fact; | |
if (type == true) { | |
return factorial(NUM-1, true) * NUM; | |
} else { | |
for (int i = 1; i <= NUM; i++) fact = fact * i; | |
return fact; | |
} | |
} | |
int main() { | |
std::cout << factorial(0, true); //recursive | |
std::cout << "\n"; | |
std::cout << factorial(0, false); //non-recursive | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment