-
Notifications
You must be signed in to change notification settings - Fork 2
/
Day 17 More Exceptions.cs
44 lines (37 loc) · 1 KB
/
Day 17 More Exceptions.cs
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
using System;
//Write your code here
class Calculator{
public int power(int n, int p){
int powerValue = 1;
try{
if(n < 0 || p < 0){
throw new Exception();
}
for(int i = 0; i < p; i++){
powerValue = n* powerValue;
}
}
catch{
throw new Exception("n and p should be non-negative");
}
return powerValue;
}
}
class Solution{
static void Main(String[] args){
Calculator myCalculator=new Calculator();
int T=Int32.Parse(Console.ReadLine());
while(T-->0){
string[] num = Console.ReadLine().Split();
int n = int.Parse(num[0]);
int p = int.Parse(num[1]);
try{
int ans=myCalculator.power(n,p);
Console.WriteLine(ans);
}
catch(Exception e){
Console.WriteLine(e.Message);
}
}
}
}