fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. long double factorial = 1.0;
  7.  
  8. cout << "Enter a positive integer: ";
  9. cin >> n;
  10.  
  11. if (n < 0)
  12. cout << "Error! Factorial of a negative number doesn't exist.";
  13. else {
  14. for(int i = 1; i <= n; ++i) {
  15. factorial *= i;
  16. }
  17. cout << "Factorial of " << n << " = " << factorial;
  18. }
  19.  
  20. return 0;
  21. }
Success #stdin #stdout 0.01s 5548KB
stdin
5
stdout
Enter a positive integer: Factorial of 5 = 120