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);
        }
    }
}



Compiler Message
Success
Input (stdin)Download
3 5
2 4
0 0
-1 -2
-1 3{-truncated-}
Download to view the full testcase
Expected OutputDownload
243
16
java.lang.Exception: n and p should not be zero.
java.lang.Exception: n or p should not be negative.
java.lang.Exception: n or p should not be negative.

Comments

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions