-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFun40.java
More file actions
25 lines (23 loc) · 654 Bytes
/
Fun40.java
File metadata and controls
25 lines (23 loc) · 654 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
package fun;
public class Fun40 {
public static void main(String[] args) {
System.out.println(Exp1(4, 3));
System.out.println(Exp1(5, 3));
System.out.println(Exp1(6, 3));
}
public static double Exp1(int z, double x) {
double sum = 1; // sum + x^i/i! + x^2/2! + x^3/3! + ...
int factorial = 1;
double pow = x;
if (z>0){
for (int i = 1; i <= z; i++) {
// pow = x^i
// factorial = i!
sum += pow / factorial;
factorial *= i;
pow *= x; // x * x
}
}
return sum;
}
}