-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise3.java
More file actions
39 lines (38 loc) · 988 Bytes
/
Exercise3.java
File metadata and controls
39 lines (38 loc) · 988 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
import java.util.Scanner;
import java.lang.*;
class Exercise3 {
public static void main(String[] args) {
double subterm=1.0;
double g;
int n=2;
double f = 1.0e-12;
double x=5.0;
Scanner myinput = new Scanner (System.in);
System.out.println("Please input an integer:");
x = myinput.nextDouble();
g = 1 + x;
while (subterm > f) {
subterm=mypower(x,n)/factorial(n);
System.out.println("N=" + n + " mypower="+mypower(x,n)+" factorial="+factorial(n));
g += subterm;
System.out.println("N=" + n + " g=" + g);
n++;
}
System.out.println(Math.exp(x) + " " + g);
double text = mypower(x, n);
}
public static double mypower(double m, int j){
double result=1.0;
for (int i = 1; i<=j; i++){
result = result * m;
}
return result;
}
public static double factorial (int j){
double result = 1.;
for (int i = 1; i<=j; i++){
result = result * i;
}
return result;
}
}