Mrain

rekurzije c++

Oct 26th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int potenciranje(int baza, int potencija)
  5. {
  6.     if (potencija==0) return 1;
  7.     else return baza * potenciranje(baza,potencija-1);
  8. }
  9.  
  10. void main()
  11. {
  12.     unsigned int p, b, r;
  13.  
  14.     cout << "Upisite bazu :" ;
  15.     cin >> b ;
  16.     cout <<  "Upisite potenciju :" ;
  17.     cin >> p ;
  18.  
  19.     r = potenciranje(b,p);
  20.  
  21.     cout << b << " na "  << p << " = " << r << endl;
  22. }
  23.  
  24. //////////////////////////////////////////////// za negativne
  25.  
  26. #include <iostream>
  27. using namespace std;
  28.  
  29. float racun(int b, int p)
  30. {
  31.         if (p == 0) return 1;
  32.         else if(p < 1) return 1 / (b*racun(b,-1-p)); // samo u prvon ophodu ide kroz ovaj else if jer se
  33.         else return b * racun(b,p-1);                // vrijednost promini u pozitivnu pa ide kroz else
  34.                                                      // do kraja
  35. }
  36.  
  37. void main()
  38. {
  39.         int b,p;
  40.         cout << "Unesite bazu :" ;
  41.         cin >> b ;
  42.         cout << "Unesite potenciju :";
  43.         cin >> p;
  44.  
  45.  
  46.         float r = racun(b,p);
  47.  
  48.         cout << b << " na " << p << " = " << r << endl;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment