hacker rank 46th problem java(java exception handling)
46. JAVA EXCEPTION HANDLING
class MyCalculator {
public int power(int n, int p) throws Exception {
if (n < 0 || p < 0) {
throw new Exception("n or p should not be negative.");
}
if(n==0&&p==0){
throw new Exception("n and p should not be zero.");
}else if (p == 0) {
return 1;
}
else if(n==0){
return 0;
}else if (p==1)
{
return n;
}
else {
return n * power(n, p - 1);
}
}
}
Comments