power(base,n) recursively in java

Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared).

powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27

------------------==========================================
public int powerN(int base, int n) {



int result=base;


if(n==0) return 1;

if(n > 0){


return result * (powerN(base,n-1));


}
return result;


}

Comments

  1. this is good post.
    and you can go here

    http://bantalsilikon01.blogspot.com
    http://kursusinternetmarketingmurah.blogspot.com
    http://bumbupecelbali.blogspot.com


    tanks very much.... :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Spring MVC- Redirect from one controller to another

Integrating Struts2 with Spring Security using Custom Login Form