home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / factor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-03  |  572 b   |  31 lines

  1. /* FACTOR.C: Demonstrate functions.
  2.  *  NOTE:  a value > 16 will overflow a long integer.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. long factor( int param );
  8.  
  9. main()
  10. {
  11.    int num;
  12.    long result;
  13.    /* Display a prompt */
  14.    printf( "Type a number: " );
  15.    /* Input a numeric value, assign to num */
  16.    scanf( "%d", &num );
  17.    result = factor( num );
  18.    printf( "Factorial of %d is %ld\n", num, result );
  19. }
  20.  
  21. long factor( int param )
  22. {
  23.    long value = 1;
  24.    while( param > 1 )
  25.    {
  26.       value = value * param;
  27.       param = param - 1;
  28.    }
  29.    return value;
  30. }
  31.