Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 17 More Exceptions 30 days of code solution

YASH PAL, 31 July 202413 October 2025

HackerRank Day 17 More Exceptions solution – In this HackerRank Day 17 More Exceptions 30 days of code problem set, we need to develop a program that can take two input integers and then we need to print the power of that inputs on the output screen and if the numbers are negative integers then we need to print the message that inputs are needed to positive numbers.

Objective
Yesterday’s challenge taught you to manage exceptional situations by using try and catch blocks. In today’s challenge, you will practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video.

Task
Write a Calculator class with a single method: int power(int,int). The power method takes two integers,n and p, as parameters and returns the integer result of np. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative.

Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.

Input Format
Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p, respectively.

Constraints
No Test Case will result in overflow for correctly written code.
Output Format

Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of np as calculated by your Calculator class’ power method.

Problem solution in Python 2 programming.

#Write your code here
class Calculator:
    def power(self, n, p):
        if (n < 0 or p < 0):
            raise Exception("n and p should be non-negative")
        else:
            return n**p
        

Problem solution in Python 3 programming.

#Write your code here
class Calculator:

    def power(self,n, p):
        if n < 0 or p < 0:
            raise Exception("n and p should be non-negative")
        else:
            return pow(n,p)

Problem solution in java programming.

class Calculator{
    public int power(int n, int p) throws Exception{
        if(n < 0 || p < 0){
            throw new Exception("n and p should be non-negative");
        }
        return (int)Math.pow(n,p);
    }
}

Problem solution in c++ programming.

//Write your code here
class Calculator {
    public:
        int power(int n, int p){
            if(n < 0 || p < 0){
                throw invalid_argument("n and p should be non-negative"); 
            }
            
            return pow(n, p);
        }
};

Problem solution in Javascript programming.

//Write your code here
function Calculator() {
    this.power = function(n, p) {
        if (n < 0 || p < 0)
            throw "n and p should be non-negative";
            
        var ans = 1;
        for (i = 0; i < p; i++) {
            ans *= n;
        }
        
        return ans;
    }
}

30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes