pow implementation

/***
Implement math pow function
***/

#include<iostream>
#include<cmath>

using namespace std;

int mypow(int a, int b) {
       if(b==0) return 1;
       if(a==0) return a; //incorrect input

       //Note: remember intialize the power
       int power = 1;
       while(b--) {
              power *= a;
       }
       return power;
}

void main() {
       int p1 = pow(-1, 9);
       int p2 = mypow(-1, 9);

       cout<<"p1:"<<p1<<"   p2:"<<p2<<endl;

}

Comments

Popular posts from this blog

Sudoku

Longest prefix matching - trie

Climbing Stairs