Computers in Engineering WWW Site - Example 12.6

Example 12.6


C Version

/*
  Factorial Program.
*/

#include <stdio.h>

main()
{
  /*  Factorial n  */

  /*  Declaration Statements  */
  short i, n;
  double fact;
  short FORLIM;

  printf("C46.C -> Factorial Program\n");

  /*  Assignment Statements  */
  printf("Enter the number you want (n!) : ");
  scanf("%hd", &n);
  getchar();
  while (n >= 0) {
    fact = 1.0;
    FORLIM = n;
    for (i = 2; i <= FORLIM; i++)
      fact *= i;

    /*  Print result  */
    printf("Factorial of %3d = %8.0f\n", n, fact);
    printf("\n-------------------------------------\n\n");
    printf("Enter the factorial number you want \n ");
    printf("(enter a negative value to stop the program) : ");
    scanf("%hd", &n);
    getchar();
  }  /*  End of while{} loop  */

  return(0);
}
/*  End of Program C46 */
/*
INPUT :

3
10
5
-1

OUTPUT :

C46.C -> Factorial Program
Enter the number you want (n!) : 3
Factorial of   3 =        6

-------------------------------------

Enter the number you want (n!)
(enter a negative value to stop the program) : 10
Factorial of  10 =  3628800

-------------------------------------

Enter the number you want (n!)
(enter a negative value to stop the program) : 5
Factorial of   5 =      120

-------------------------------------

Enter the number you want (n!)
(enter a negative value to stop the program) : -1

*/

Last modified: 21/07/97