XeBuZer0

Factorial in C with iterations and ARGC/ARGV

Dec 20th, 2019 (edited)
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** //
  2. // ** // ** // ** // Código fuente de programa que calcula el factorial de un // ** // ** // ** //
  3. // ** // ** // ** // ** // número natural dado usando iteraciones // ** // ** // ** // ** // ** //
  4. // ** // ** // ** // * Licenciado bajo GNU General Public License (GPL) 3.0 * // ** // ** // ** //
  5. // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** //
  6. /* ** // ** // ** // ** // ** //  * F v q _ U k r a N a z i s ! * // ** // ** // ** // ** // ** */
  7. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13.  
  14. long double fact(long double a){
  15.     register long double cont=1;
  16.     long double acum=1;
  17.     if (0<a) for (cont=1;cont<a;cont++)  acum = acum * cont;
  18.     return acum;
  19. }
  20.  
  21. void main(int argc, const char **argv){
  22.     long double x=0;
  23.     if (argc != 2){
  24.         extern int errno;
  25.         errno = 22;
  26.         fprintf(stderr, "Uso: %s <NÚMERO NATURAL>\n Code error: %d\n %s\n",*(argv+0), errno, strerror(errno));
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     sscanf( *(argv+1) , "%Lf", (long double*) &x);
  30.     if (0<x) printf("El factorial de %4.0Lf es %4.0Lf \n", x, fact(x));
  31. }
Add Comment
Please, Sign In to add comment