#include<iostream>

// Simple recursive factorial using a native type (unsigned long long).
// Note: overflow for n > 20 on 64-bit unsigned long long.
unsigned long long fact_recursive(unsigned n) {
return (n <= 1) ? 1ULL : n * fact_recursive(n – 1);
}

int main() {
unsigned n;
std::cout << “Enter a non-negative integer: “; if (!(std::cin >> n)) return 1;

if (n > 20) {
std::cout << “Warning: result will overflow unsigned long long for n > 20.” << std::endl;
}

unsigned long long result = fact_recursive(n);
std::cout << n << “! = ” << result << std::endl;
return 0;
}

Shopping Cart
Optimized by Optimole
Scroll to Top