Solved

# powerN  challenge

Posted on 2016-09-09
81 Views
Hi,

I am working on below challenge
http://codingbat.com/prob/p158888

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
i was not clear on how to find the powerN using recursion? please advise
0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 27

Accepted Solution

rrz earned 500 total points
ID: 41792204
This challenge is very similar to the other challenges that you started to the same time. I can give you two hints about the base case here.  Every number to the power of 0 is equal to one.  Furthermore, every number multiplied by 1 is equal to that same number.
0

LVL 7

Author Comment

ID: 41793661
``````public int powerN(int base, int n) {
if(n==0) return 1;
//  if(n==1) return base;
else{
return base*powerN(base,n-1);
}
}
``````
above passes all test. any alternate approaches or improvements?
0

LVL 27

Expert Comment

ID: 41793740
My solution is the same as yours.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

convert Systemjs to Webpack 3 121
Octopus Deploy: need a Time Event that fires every midnight 2 55
DTD and JAVA versions 1 53