HackerRank Java Exception Handling problem solution

In this HackerRank Java Exception Handling problem solution, you need to Complete the function power in class MyCalculator and return the appropriate result after the power operation or an appropriate exception.

HackerRank Java Exception Handling problem solution

HackerRank Java Exception Handling problem solution

class myCalculator{
public int power(int n, int p) throws Exception{
if(n < 0 || p < 0)
throw(new Exception("n and p should be non-negative"));
int product =1;
for(int i =0; i <p; i++)
product *= n;

return product;

}
}


Post a Comment

0 Comments