Advertisement
Jakzon123

factorial func

Oct 15th, 2022
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int n;
  6.     long 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement