/*
 * factoral.cpp
 *
 * This program reads an integer and prints out the factorial of that
 * integer.  The factorial can be a very large number because internally
 * the BigNum class is used to store integers.
 *
 * User interaction is handled by main, arbitrary precision arithmetic is
 * handled by the BigNum class.
 *
 */

#include <iostream>
#include <string>
using namespace std;

#include "bigint.hpp"

/* factorial [global function]:
 * 	Inputs:	 BigInt n    
 *	Returns: BigInt			n! (factorial of n) 		
 * 
 * Simpile iterative factorial of n.  n must not be negative or zero.
 */

BigInt factorial(BigInt n)
{
    BigInt result(1);
    while (n != BigInt(1)) {
        result *= n;
	n -= BigInt(1);
    }
    return result;
}


/* main [global function & program entry point]:
 * 	Inputs:	 int UNUSED    		command-line argument count +1 [unused]
 *		 const char* UNUSED	command-line arguments [unused]
 *	Returns: int			exit code
 * 
 * Read lines from the user until a blank line is entered and then outputs
 * those lines in random order.
 */

int main (int, const char* [])
{
    BigInt x(1234);
    BigInt y(5678);
    x *= y;
    cout << "x = " << x << ", y = " << y << endl;
    cout << "Enter a number for factorial: ";

    int n;
    cin >> n;
    BigInt bigN(n);
    cout << "Factorial is " << factorial(bigN) << endl;

    return 0;			// Exit code for "successful execution".
}


/* Junk to make emacs use the right C++ style when editing this code:
 *   Local Variables:
 *   c-file-style: "stroustrup"
 *   End:
 */
