Skip to content

Instantly share code, notes, and snippets.

@zuigon
Created December 29, 2009 20:10
Show Gist options
  • Save zuigon/265557 to your computer and use it in GitHub Desktop.
Save zuigon/265557 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment