/* Program 8.4.1 from C++ Programming Lecture notes  */

/* Author: Rob Miller and William Knottenbelt
   Program last changed: 28th September 2013    */

/* This program calculates the factorial of a given positive integer. */ 

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

/* Function which calculates the factorial of its non-negative integer argument */
int factorial(int number);

/* MAIN PROGRAM: */
int main()
{
	int whole_number;
	
	cout << "Enter a positive integer:\n";
	cin >> whole_number;
	cout << "The factorial of " << whole_number << " is ";
	cout << factorial(whole_number) << ".\n";
	
	return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION DEFINITION OF factorial: */
int factorial(int number)
{
	if (number < 0)
	{
		cout << "\nError - negative argument to factorial\n";
		exit(1);
	}
	else if (number == 0)
		return 1;
	else
		return (number * factorial(number - 1));
}
/* END OF FUNCTION */
