Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. //==============================================================================
  2. ///
  3. /// File:           fac2.cpp
  4. /// Author:         Gerald Broughton
  5. /// Date Created:   10/25/2011
  6. /// Changes:        -none-
  7. ///
  8. //==============================================================================
  9.  
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. double factorial(double n){
  15.     double ans = 1;
  16.     for(double i = 2; i <= n; ++i){
  17.         ans *= i;
  18.         if(ans < 0 || ans > 7.25742e+306)
  19.             return -1;
  20.     }
  21.     return ans;
  22. }
  23.  
  24. int main(){
  25.     cout << "Factorial of: " << flush;
  26.     double n;
  27.     if(cin >> n){
  28.         if(n >= 0 && n != 9999){
  29.             double fact = factorial(n);
  30.             if(fact < 0){
  31.                 cerr << "overflow error: " << n << " is too big." << endl;
  32.             }else{
  33.                 cout << n << "! is " << fact << endl;
  34.             }
  35.         }else{
  36.             cerr << "\aUndefined: factorial of a negative number " << n << endl;
  37.         }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment