fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. long long factorial = 1; // Using long long for handling larger values
  7.  
  8. cout << "Enter a non-negative integer: ";
  9. cin >> n;
  10.  
  11. if (n < 0) {
  12. cout << "Factorial is not defined for negative numbers." << endl;
  13. } else {
  14. for (int i = 1; i <= n; ++i) {
  15. factorial *= i; // Multiplying with each number up to n
  16. }
  17. cout << "Factorial of " << n << " = " << factorial << endl;
  18. }
  19.  
  20. return 0;
  21. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter a non-negative integer: Factorial of 22076 = 0