Simple method to calculate power of a number without any extern function

Viewed 21

I look for a simple method for calculating the power of a number without using any extern function, only one loop and one if/else condition. It takes only an integer for power (positive, negative and zero). I make this function in pseudo language if anybody knows another method?

function power(x,n)
{
    var index, result=1;
     
        if (n<0) then
        {
            x=1/x; 
            n=-n;
        }
        
        foreach index from 0 to n
        {
            result=result*x;
        }
       
      return result;        
}
0 Answers
Related