Last active
July 18, 2021 06:02
-
-
Save eventhelix/c46862312f6a57676285c82afaab836c to your computer and use it in GitHub Desktop.
Example of a recursive factorial function
This file contains 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
#include <array> | |
#include <cstdint> | |
#include <cstdio> | |
constexpr std::uint64_t factorial(std::uint64_t n) { | |
// Recursion termination condition | |
if (n == 0) | |
{ | |
return 1; | |
} | |
return n * factorial(n - 1); | |
} | |
void printFactorial(std::uint64_t n) | |
{ | |
printf("%lu!=%lu\n", n, factorial(n)); | |
} | |
constexpr std::array num{16ull, 9ull}; | |
int main() { | |
printFactorial(5); | |
for (const int n : num) | |
{ | |
printFactorial(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment