fork download
  1. /* Write a C++ program to calculate the factorial of a number */
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main() {
  7. int n;
  8. long long factorial = 1;
  9.  
  10. cout << "Enter a positive integer: ";
  11. cin >> n;
  12.  
  13. if (n < 0) {
  14. cout << "Factorial is not defined for negative numbers." << endl;
  15. } else {
  16. for (int i = 1; i <= n; i++) {
  17. factorial *= i;
  18. }
  19. cout << "Factorial of " << n << " = " << factorial << endl;
  20. }
  21.  
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter a positive integer: Factorial of 21942 = 0