Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==============================================================================
- ///
- /// File: fac2.cpp
- /// Author: Gerald Broughton
- /// Date Created: 10/25/2011
- /// Changes: -none-
- ///
- //==============================================================================
- #include <iostream>
- using namespace std;
- double factorial(double n){
- double ans = 1;
- for(double i = 2; i <= n; ++i){
- ans *= i;
- if(ans < 0 || ans > 7.25742e+306)
- return -1;
- }
- return ans;
- }
- int main(){
- cout << "Factorial of: " << flush;
- double n;
- if(cin >> n){
- if(n >= 0 && n != 9999){
- double fact = factorial(n);
- if(fact < 0){
- cerr << "overflow error: " << n << " is too big." << endl;
- }else{
- cout << n << "! is " << fact << endl;
- }
- }else{
- cerr << "\aUndefined: factorial of a negative number " << n << endl;
- }
- }
- }
Add Comment
Please, Sign In to add comment