-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecPower.java
More file actions
40 lines (30 loc) · 802 Bytes
/
RecPower.java
File metadata and controls
40 lines (30 loc) · 802 Bytes
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
/*
* Title: RecPower.java
* Description: Find the power of a number using a recursive function
* Author: Nithun Harikrishnan
* Version: 1.0
* Usage: >javac RecPower.java >java RecPower
*/
package week2;
import java.util.Scanner;
public class RecPower {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double x1;
int n1;
double ans;
System.out.println("Enter value of x:");
x1 = scnr.nextDouble();
System.out.println("Enter value of n:");
n1 = scnr.nextInt();
ans = power(x1,n1); // calling power function
System.out.printf("The value of x^n is: "+ ans);
scnr.close();
}
public static double power(double x, int n){ // power function
if (n == 0)
return 1.0;
else
return (power(x,n-1)*x); //recursive call
}
}