/* Factfind.c */
/* A program that calculates the factorial of an integer recursively. */

#include<stdio.h>  /* necessary for stream I/O */

/* function prototype */
long factorial(long n);

int main()
{
 long n;       /* number entered by user */
 long result;  /* result returned by factorial function */

 printf("Enter a non-negative integer: ");
 scanf("%ld", &n);
 if(n == 0) /* If the integer is 0, print message and do no calculation. */
  {
   printf("By definition, the factorial of 0 is 1.\n");
  }
 else
  {
   if(n > 0) /* If the integer is greater than zero, calculate */
    {        /* the factorial. */
     result = factorial(n);  /* Call the recursive function. */
     printf("The factorial of %ld is %ld.\n", n, result);
    }
   else    /* If the integer is less than zero, entry is not valid. */
    {
     printf("Not a valid integer.\n");
    }
  }
 return 0;
}

/* Recursive function that calculates factorial. */
long factorial(long n)
{
 long fact;   /* Local variable returned by function. */

 if(n > 1)
  {          /* If n is not one, make another recursive call. */
   fact = n * factorial(n - 1);
  }
 else
  {          /* Exit condition */
   fact = 1; /* If n is one, recursion stops and flow of logic */
  }          /* begins "backing out" of recursive calls. */
 return(fact);  /* Return fact to the next level of recursion. */
}

