Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int
- fact(int n){
- if(n==0) return 1;
- else return n*fact(n-1);
- }
- int
- pot1(int n, int p){
- if(p==0) return 1;
- if(p==1) return n;
- else return n*pot1(n,p-1);
- }
- int
- pot2(int x, int n){
- if(n==0) return 1; // -> segm. fault
- if(n==1) return x;
- if(n%2==0){
- int b = pot2(x, n/2);
- return b*b;
- }
- else return x*pot2(x,n-1);
- }
- int
- main(int argc, char** argv){
- int n,p;
- cin >> n;
- cin >> p;
- // Faktorijel
- cout << fact(n)<<endl<<endl;
- // Potencije
- cout << pot1(n,p)<<endl;
- cout << pot2(n,p)<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment