Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python

C++


JAVA
PHP
SQL
Alice

Factorial


Factorial.cpp

//-------------------------------------------------------------------------
// Factorial.cc
//-------------------------------------------------------------------------
// Usage:
//
// Factorial n
//
// where 'n' is a positive integer.
// Print out n!.
//
// Charles A. Cusack, May 1999
//-------------------------------------------------------------------------
#include "INTEGER.h"
#include<cstdlib>
using namespace std;
//-------------------------------------------------------------------------
INTEGER Factorial(INTEGER N);
//-------------------------------------------------------------------------
int main (int argc,char* argv[]) {

if(argc!=2 ) {       // Check for correct number of command line arguments.
    cout<<"USAGE: "<<argv[0]<<" number\n"; 
    exit(1); 
    }

INTEGER N=atoi(argv[1]);
INTEGER FactN=Factorial(N);
cout<<FactN<<"\n";
cout<<"It took "<<INTEGER::get_total()<<" operations to compute\n";
cout<<"They break down as follows:\n";
INTEGER::print_stats();
return 0;
}
//-------------------------------------------------------------------------
INTEGER Factorial(INTEGER N) {
        if (N <= 1)
           return(1);
        else
           return N*Factorial(N-1);
}
//-------------------------------------------------------------------------