-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday17.cpp
57 lines (49 loc) · 1.45 KB
/
day17.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Tutorial: 30 Days of Code.
* A solution to "Day 17: More Exceptions"
* Submitted by A. S. "Aleksey" Ahmann <[email protected]>
* Submitted on Feb. 29, 2024
* Link: https://www.hackerrank.com/challenges/30-more-exceptions/problem
*
* Task description: Write a Calculator class with a single method: int power(int,int). The power method takes two integers, m and p, as parameters and returns the integer result of .
* If either n or p is negative, then the method must throw an exception with the message: "n and p should be non-negative".
*
*/
#include <cmath>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
//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");
int y = 1;
for (int i = 0; i < p; i++)
y *= n;
return y;
}
};
int main()
{
Calculator myCalculator=Calculator();
int T,n,p;
cin>>T;
while(T-->0){
if(scanf("%d %d",&n,&p)==2){
try{
int ans=myCalculator.power(n,p);
cout<<ans<<endl;
}
catch(exception& e){
cout<<e.what()<<endl;
}
}
}
}
/* Endnotes:
* 1. I referenced the following resources when writing this solution:
* 1a. https://rollbar.com/blog/error-exceptions-in-c/#
*/