Skip to content

Commit 4770692

Browse files
add day 17
1 parent e047ec9 commit 4770692

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package hackerRank.thirtyDaysOfCode;
2+
3+
import java.util.*;
4+
// More Exceptions
5+
class Day_17{
6+
public static void main(String[] args) {
7+
Scanner in = new Scanner(System.in);
8+
int t = in.nextInt();
9+
while (t-- > 0) {
10+
11+
int n = in.nextInt();
12+
int p = in.nextInt();
13+
Calculator myCalculator = new Calculator();
14+
try {
15+
int ans = myCalculator.power(n, p);
16+
System.out.println(ans);
17+
}
18+
catch (Exception e) {
19+
System.out.println(e.getMessage());
20+
}
21+
}
22+
in.close();
23+
}
24+
25+
private static class Calculator {
26+
private int power(int n, int p) throws Exception {
27+
if (n < 0 || p < 0) {
28+
throw new Exception("n and p should be non-negative");
29+
}
30+
if (p == 0) {
31+
return 1;
32+
}
33+
int res = n;
34+
for (int i = 1; i < p; i++) {
35+
res = res * n;
36+
}
37+
return res;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)