Skip to content

Commit 20a36a0

Browse files
Power of a number
Calculate the power of a number using recursion
1 parent efbdc75 commit 20a36a0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Problem1Recursion.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Calculate the power of a number using recursion
2+
3+
package problem1.recursion;
4+
5+
import java.util.*;
6+
7+
public class Problem1Recursion {
8+
9+
public static void main(String[] args) {
10+
11+
Scanner input = new Scanner(System.in);
12+
13+
System.out.print("Enter a Number : ");
14+
int n = input.nextInt();
15+
16+
System.out.print("Enter a Power : ");
17+
int p = input.nextInt();
18+
19+
System.out.println(power(n, p));
20+
}
21+
22+
static int power(int n, int p) {
23+
if (p == 0) {
24+
return 1;
25+
} else {
26+
return n * power(n, p - 1);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)