limun11

Untitled

Sep 19th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. /*Napišite program, koji omogućava unos prirodnog broja n preko tastature te izračunava sumu:
  4.  
  5. S= 1!/1/2  +2!/1/2+1/3     ….+  n!/1/2+1/3+...+1/n
  6. Upotrijebite funkcije:
  7. int faktor (int);
  8. float suma(int);
  9. float ukupnasuma(int);
  10. Unos prirodnog broja i ispis ukupne sume vršiti u funkciji main.
  11. */
  12.  
  13. int faktor(int);
  14. float suma(int);
  15. float ukupnasuma(int);
  16. int main()
  17. {
  18.  
  19.     int n;
  20.     cout << "Unesite n: " << endl;
  21.     cin >> n;
  22.    
  23.     while (n < 0)
  24.     {
  25.         cout << "Broj koji ste unijeli nije prirodan. Ponovite unos: " << endl;
  26.         cin >> n;
  27.     }
  28.    
  29.     cout << "Suma iznosi: " << ukupnasuma(n) << endl;
  30.  
  31.     system("PAUSE");
  32.     return 0;
  33. }
  34.  
  35. int faktor(int n)
  36. {
  37.     int faktorijel = 1;
  38.     for (int i = 1; i <=n; i++)
  39.     {
  40.         faktorijel = faktorijel*i;
  41.     }
  42.     return faktorijel;
  43. }
  44.  
  45. float suma(int n)
  46. {
  47.     float sum = 0;
  48.     for (int i = 2; i <= n; i++)
  49.     {
  50.         sum = sum + (1 /2 + 1/(float) i);
  51.     }
  52.     return sum;
  53. }
  54.  
  55. float ukupnasuma(int n)
  56. {
  57.     float usum = 0;
  58.    
  59.             usum = usum + faktor(n) / suma(n);
  60.        
  61.     return usum;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment