marwanpro

tp2 -- part 2

Sep 19th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #define SPACE " "
  7.  
  8. using namespace std;
  9.  
  10. // Init Void
  11. int random(int min, int max);
  12. int fibonacci(int n);
  13. double factorielle(int n);
  14. int combinaison(int n, int p);
  15. void trianglePascal(int n);
  16.  
  17. int main(void)
  18. {
  19.     trianglePascal(4);
  20.     system("pause");
  21.     return 0;
  22. }
  23.  
  24. int random(int min, int max)
  25. {
  26.     if (min > max)
  27.     {
  28.         int temp = max;
  29.         max = min;
  30.         min = temp;
  31.     }
  32.     cout << min << SPACE << max << endl;
  33.     srand(time(NULL));
  34.     int val = (rand() % (max - min + 1) + min);
  35.     return val;
  36. }
  37.  
  38. int fibonacci(int n)
  39. {
  40.     if (n <= 1) return n;
  41.     else return fibonacci(n - 1) + fibonacci(n - 2);
  42. }
  43.  
  44. double factorielle(int n)
  45. {
  46.     int f = 1;
  47.     for (int i = 1; i < n + 1; i++)
  48.     {
  49.         f *= i;
  50.     }
  51.     return f;
  52. }
  53.  
  54. int combinaison(int n, int p)
  55. {
  56.     return factorielle(n) / (factorielle(p) * factorielle(n - p));
  57. }
  58.  
  59. void trianglePascal(int n)
  60. {
  61.     cout << "Triangle de Pascal --- n = " << n << endl << 1 << endl;
  62.     for (int i = 1; i < n + 1; i++)
  63.     {
  64.         for (int j = 0; j < i + 1; j++)
  65.         {
  66.             int currentVal = combinaison(i, j);
  67.             cout << currentVal << SPACE;
  68.         }
  69.         cout << endl;
  70.     }
  71. }
Add Comment
Please, Sign In to add comment