9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia01/ej8.c (407B)
   1 /*    Calculates the factorial of a given number.    */
   2 
   3 #include <stdio.h>
   4 
   5 #define ERR_MSG_NEG "ERR: The entered number is negative!"
   6 
   7 int main(void) {
   8     
   9     int fact, i, res;
  10 
  11     printf("Enter a number: ");
  12     if(scanf("%i", &fact) == EOF)
  13         return 1;        
  14 
  15     if(fact < 0) {
  16         printf(ERR_MSG_NEG"\n");
  17         return 1;
  18     }
  19 
  20     res = 1;
  21     for(i = 1; i <= fact; i++) {
  22         res *= i;
  23     }
  24 
  25     printf("%d! = %d\n", fact, res);
  26     return 0;
  27 }