c-first-steps

a C playground
Index Commits Files Refs README
the-c-programming-language/factorial.c (384B)
   1 /*    Calculates the factorial of a given number.    */
   2 
   3 #include <stdio.h>
   4 
   5 int main(void) {
   6     
   7     int fact, i, res;
   8 
   9     printf("Enter a number: ");
  10     scanf("%i", &fact);            // future update, delete this unsecure function 
  11 
  12     if(fact < 0) {
  13         printf("Enter a valid number!\n");
  14         return 1;
  15     }
  16 
  17     res = 1;
  18     for(i = 1; i <= fact; i++) {
  19         res *= i;
  20     }
  21 
  22     printf("%d! = %d\n", fact, res);
  23     return 0;
  24 }