/* Filename:  fact.cpp

   Author:  Br. David Carlson

   Date:  March 8, 1998

   Last Revised:  November 27, 2001

   This program allows the user to repeatedly find the factorial of a
   non-negative integer.  Each time, the user is prompted to enter the
   desired integer and its factorial is then printed on the screen.

   Tested with:
      Microsoft Visual C++ 6.0
      Microsoft Visual C++ .NET
      g++ under Linux
*/

#include <iostream>
using namespace std;


// Function prototype:
long factorial(int n);


int main(void)
   {
   int n;

   cout << "Enter a (not too large) non-negative integer or -1 to quit: ";
   cin >> n;

   while (n != -1)
      {
      cout << "The factorial of " << n << " is " << factorial(n)
         << endl << endl;

      cout << "Enter a (not too large) non-negative integer"
         << " or -1 to quit: ";
      cin >> n;
      }

   return 0;
   }


/* Given:  n  A non-negative integer.
   Task:   To compute the factorial of n.
   Return: This factorial in the function name.
*/
long factorial(int n)
   {
   if ((n == 0) || (n == 1))
      return 1;
   else
      return n * factorial(n - 1);
   }


