File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
src/main/java/HackerRank/thirtyDaysOfCode Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments