Skip to content

Instantly share code, notes, and snippets.

@bhu1st
Last active July 5, 2016 23:17
Show Gist options
  • Save bhu1st/42ce14e7d6df06fa5495c8278dfb507f to your computer and use it in GitHub Desktop.
Save bhu1st/42ce14e7d6df06fa5495c8278dfb507f to your computer and use it in GitHub Desktop.
/*
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