adnanj

Suma faktorijela do n

Dec 19th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int Faktorijel(int);
  5. int Suma(int);
  6.  
  7. int main() {
  8.     int n;
  9.  
  10.     do {
  11.     cout << "Unesite prirodan broj n: ";
  12.     cin >> n;
  13.     }
  14.     while(n<=0);
  15.  
  16.     for(int i = 1; i <= n; i++) {
  17.     cout << i << "!";
  18.     if(i<n) cout << " + ";
  19.     else cout << " = " << Suma(n);
  20.     }
  21.  
  22.     system("pause>0");
  23.     return 0;
  24. }
  25.  
  26. int Faktorijel(int n) {
  27.         if(n<=1)
  28.     return 1;
  29.     return n*Faktorijel(n-1);
  30. }
  31.  
  32. int Suma(int n) {
  33.         int suma = 0;
  34.     for(int i = 1; i <= n; i++) {
  35.     suma += Faktorijel(i);
  36.     }
  37.     return suma;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment