-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerFunction.text
More file actions
51 lines (44 loc) · 1.12 KB
/
powerFunction.text
File metadata and controls
51 lines (44 loc) · 1.12 KB
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
/*Take two integers,n and p ,print the integer result of n^p.
If either n or p is negative,print "n and p should be non-negative."*/
import java.util.*;
import java.io.*;
class Calculator
{
int n;
int p;
int power(int n, int p) throws Exception
{
if(n<0 || p<0)
{
throw new Exception("n and p should be non-negative");
}
else
{
int ans = 1;
for(int i =1; i<=p; i++)
{
ans = ans*n;
}
return ans;
}
}
}
class Solution{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int p = in.nextInt();
Calculator myCalculator = new Calculator();
try {
int ans = myCalculator.power(n, p);
System.out.println(ans);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
in.close();
}
}