Skip to content

Instantly share code, notes, and snippets.

@eventhelix
Last active July 18, 2021 06:02
Show Gist options
  • Save eventhelix/c46862312f6a57676285c82afaab836c to your computer and use it in GitHub Desktop.
Save eventhelix/c46862312f6a57676285c82afaab836c to your computer and use it in GitHub Desktop.
Example of a recursive factorial function
#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