Guest User

Untitled

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int
  5. fact(int n){
  6. if(n==0) return 1;
  7. else return n*fact(n-1);
  8. }
  9.  
  10. int
  11. pot1(int n, int p){
  12. if(p==0) return 1;
  13. if(p==1) return n;
  14. else return n*pot1(n,p-1);
  15. }
  16.  
  17. int
  18. pot2(int x, int n){
  19. if(n==0) return 1; // -> segm. fault
  20. if(n==1) return x;
  21.  
  22. if(n%2==0){
  23. int b = pot2(x, n/2);
  24. return b*b;
  25. }
  26. else return x*pot2(x,n-1);
  27. }
  28.  
  29.  
  30. int
  31. main(int argc, char** argv){
  32. int n,p;
  33. cin >> n;
  34. cin >> p;
  35.  
  36. // Faktorijel
  37. cout << fact(n)<<endl<<endl;
  38.  
  39. // Potencije
  40. cout << pot1(n,p)<<endl;
  41. cout << pot2(n,p)<<endl;
  42.  
  43. return 0;
  44. }
Add Comment
Please, Sign In to add comment