home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 2.ddi / FACTOR.ZIP / FACTOR.CPP next >
Encoding:
C/C++ Source or Header  |  1990-07-14  |  1.5 KB  |  62 lines

  1. //
  2. //  C++ program to calculate straight factorals.
  3. //
  4.  
  5. #include<iostream.h>
  6. #include<stdlib.h>
  7.  
  8. /* Function ProtoTypes ----------------------------------------------------*/
  9.  
  10. double factorial(int answer);
  11. void   error_msg(int errtype);
  12.  
  13. /*-------------------------------------------------------------------------*/
  14. void main(int argc, char *argv[])
  15. {
  16.    int number;
  17.    double fact;
  18.  
  19.    cout << "\nC++ Factorial Calculator";
  20.    cout << "\nWritten by William J. Klos [73077,1601]\n";
  21.  
  22.       if(argc <= 1)                 // If not enough arguments, display
  23.     error_msg(0);               // error message.
  24.       else
  25.     number = atoi(argv[1]);
  26.  
  27.       if(number > 170)
  28.     error_msg(1);
  29.  
  30.    fact=factorial(number);
  31.  
  32.    if(fact >= 10000000)             // If factorial > ten million, then
  33.      cout.setf(ios::scientific);    // display in scientific notation.
  34.    else
  35.      cout.setf(ios::fixed);
  36.  
  37.    cout << "\nThe factorial of " << number << " is " << fact;
  38. }
  39.  
  40. /*-------------------------------------------------------------------------*/
  41. double factorial(int answer)
  42. {
  43.    if(answer <= 1)
  44.      return(1);
  45.    else
  46.      return(answer*factorial(answer-1));
  47. }
  48.  
  49. /*-------------------------------------------------------------------------*/
  50. void error_msg(int errtype)
  51. {
  52.     switch(errtype)
  53.     {
  54.       case 0: cout << "\nERROR:No parameters were entered.";
  55.           break;
  56.       case 1: cout << "\nERROR: Invalid numeric parameter.";
  57.           break;
  58.     }
  59.     cout << "\nSyntax: FACTOR n  (where n is an integer (1-170)\n";
  60.     exit(1);
  61. }
  62.