neogz

Suma neparnih od 1-n

Apr 1st, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.28 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int f(int n)
  5. {
  6.     if (n <= 1)return 1;
  7.     else return n+f(n-2);
  8.    
  9. }
  10.  
  11.  
  12. int main()
  13. {
  14.     int n;
  15.     cout << "Unesi n: ";
  16.     cin >> n;
  17.     if (n % 2 == 0)n--;
  18.  
  19.     cout << "Suma neparnih od 1 do n = " << f(n);
  20.  
  21.     system("pause >nul");
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment