Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /*Napišite program, koji omogućava unos prirodnog broja n preko tastature te izračunava sumu:
- S= 1!/1/2 +2!/1/2+1/3 ….+ n!/1/2+1/3+...+1/n
- Upotrijebite funkcije:
- int faktor (int);
- float suma(int);
- float ukupnasuma(int);
- Unos prirodnog broja i ispis ukupne sume vršiti u funkciji main.
- */
- int faktor(int);
- float suma(int);
- float ukupnasuma(int);
- int main()
- {
- int n;
- cout << "Unesite n: " << endl;
- cin >> n;
- while (n < 0)
- {
- cout << "Broj koji ste unijeli nije prirodan. Ponovite unos: " << endl;
- cin >> n;
- }
- cout << "Suma iznosi: " << ukupnasuma(n) << endl;
- system("PAUSE");
- return 0;
- }
- int faktor(int n)
- {
- int faktorijel = 1;
- for (int i = 1; i <=n; i++)
- {
- faktorijel = faktorijel*i;
- }
- return faktorijel;
- }
- float suma(int n)
- {
- float sum = 0;
- for (int i = 2; i <= n; i++)
- {
- sum = sum + (1 /2 + 1/(float) i);
- }
- return sum;
- }
- float ukupnasuma(int n)
- {
- float usum = 0;
- usum = usum + faktor(n) / suma(n);
- return usum;
- }
Advertisement
Add Comment
Please, Sign In to add comment