home *** CD-ROM | disk | FTP | other *** search
- //
- // C++ program to calculate straight factorals.
- //
-
- #include<iostream.h>
- #include<stdlib.h>
-
- /* Function ProtoTypes ----------------------------------------------------*/
-
- double factorial(int answer);
- void error_msg(int errtype);
-
- /*-------------------------------------------------------------------------*/
- void main(int argc, char *argv[])
- {
- int number;
- double fact;
-
- cout << "\nC++ Factorial Calculator";
- cout << "\nWritten by William J. Klos [73077,1601]\n";
-
- if(argc <= 1) // If not enough arguments, display
- error_msg(0); // error message.
- else
- number = atoi(argv[1]);
-
- if(number > 170)
- error_msg(1);
-
- fact=factorial(number);
-
- if(fact >= 10000000) // If factorial > ten million, then
- cout.setf(ios::scientific); // display in scientific notation.
- else
- cout.setf(ios::fixed);
-
- cout << "\nThe factorial of " << number << " is " << fact;
- }
-
- /*-------------------------------------------------------------------------*/
- double factorial(int answer)
- {
- if(answer <= 1)
- return(1);
- else
- return(answer*factorial(answer-1));
- }
-
- /*-------------------------------------------------------------------------*/
- void error_msg(int errtype)
- {
- switch(errtype)
- {
- case 0: cout << "\nERROR:No parameters were entered.";
- break;
- case 1: cout << "\nERROR: Invalid numeric parameter.";
- break;
- }
- cout << "\nSyntax: FACTOR n (where n is an integer (1-170)\n";
- exit(1);
- }
-