Advertisement
huutho_96

Demo C/C++

Dec 2nd, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int bai_1(int n)
  5. {
  6.     int s = 0;
  7.     for (int i = 1; i <= n; i++)
  8.         s += i;
  9.     return s;
  10. }
  11.  
  12. int bai_2(int n)
  13. {
  14.     int s = 0;
  15.     for (int i = 1; i <= n; i++)
  16.         s += i * i;
  17.     return s;
  18. }
  19.  
  20. float bai_3(int n)
  21. {
  22.     float s = 0;
  23.     for (int i = 1; i <= n; i++)
  24.         s += 1.0f / i;
  25.     return s;
  26. }
  27.  
  28. float bai_4(int n)
  29. {
  30.     float s = 0;
  31.     for (int i = 1; i <= n; i++)
  32.         s += 1.0f / (2 * i);
  33.     return s;
  34. }
  35.  
  36.  
  37. float bai_5(int n)
  38. {
  39.     float s = 0;
  40.     for (int i = 0; i <= n; i++)
  41.         s += 1.0f / (2 * i + 1);
  42.     return s;
  43. }
  44.  
  45. float bai_6(int n)
  46. {
  47.     float s = 0;
  48.     for (int i = 1; i <= n; i++)
  49.         s += 1.0f / (i * (i + 1));
  50.     return s;
  51. }
  52.  
  53. float bai_7(int n)
  54. {
  55.     float s = 0;
  56.     for (int i = 1; i <= n; i++)
  57.         s += i * 1.0f / (i + 1);
  58.     return s;
  59. }
  60. float bai_8(int n)
  61. {
  62.     float s = 0;
  63.     for (int i = 1; i <= n; i++)
  64.         s += (2 * i + 1) * 1.0f / (2 * i + 2);
  65.     return s;
  66. }
  67.  
  68.  
  69. void main()
  70. {
  71.     int n = 5;
  72.     cout << "Bai 1: \n\tn = " << n << "\n\tKet qua: " << bai_1(n) << endl;
  73.     cout << "Bai 2: \n\tn = " << n << "\n\tKet qua: " << bai_2(n) << endl;
  74.     cout << "Bai 3: \n\tn = " << n << "\n\tKet qua: " << bai_3(n) << endl;
  75.     cout << "Bai 4: \n\tn = " << n << "\n\tKet qua: " << bai_4(n) << endl;
  76.     cout << "Bai 5: \n\tn = " << n << "\n\tKet qua: " << bai_5(n) << endl;
  77.     cout << "Bai 6: \n\tn = " << n << "\n\tKet qua: " << bai_6(n) << endl;
  78.     cout << "Bai 7: \n\tn = " << n << "\n\tKet qua: " << bai_7(n) << endl;
  79.     cout << "Bai 8: \n\tn = " << n << "\n\tKet qua: " << bai_8(n) << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement