Advertisement
Xioth

C - TP1 - factorielle.c

Jan 17th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Iterative
  5. long long int factorielle(int n)
  6. {
  7.     long long int x = 1;
  8.  
  9.     for(n; n > 0; n--)
  10.         x *= n;
  11.  
  12.     return x;
  13. }
  14.  
  15. // Récursive
  16. int factorielleR(int n)
  17. {
  18.     if(n == 1 || n == 0)
  19.         return 1;
  20.     else if (n < 0)
  21.         exit(EXIT_FAILURE);
  22.  
  23.     return n * factorielleR(n - 1);
  24. }
  25.  
  26. int main()
  27. {
  28.     int i;
  29.  
  30.     // Le résultat devient faux à partir de 17! (inclus) en utilisant un int.
  31.     // On peut remplacer int factorielle(int n) par long long int factorielle(int n)
  32.     // On peut aussi utiliser la Librairie GMP disponible sur https://gmplib.org
  33.     for(i = 0; i < 100; i++)
  34.         printf("%d! = %lld\n", i, factorielle(i));
  35.  
  36.     // Récursive
  37.     printf("10! = %d\n", factorielleR(10));
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement