-
Notifications
You must be signed in to change notification settings - Fork 0
/
myMath.java
41 lines (40 loc) · 1.14 KB
/
myMath.java
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
import java.util.*;
public class myMath {
public static float e = 2.718281828459045f;
public static float power(float x, int y){
float z = 1f;
for (int i = 0; i < y; i++){
z = z * x;
}
return z;
}
public static int fact(int x){
int y = 1;
for (int i = x; i > 0; i--){
y = y * i;
}
return y;
}
public static void main(String[] args){
Scanner sn = new Scanner(System.in);
System.out.print("Enter the value of x: ");
float x = sn.nextFloat();
System.out.println("");
System.out.print("Enter the va;lue of n: ");
int n = sn.nextInt();
sn.close();
if(n < 1 || x < 0){
System.out.println("X and N must be positive");
System.exit(1);
}
float result = 1;
for(int i = n; i > 1; i--){
float z = power(x, i - 1);
float w = fact(i - 1);
float v = z / w;
result = result + v;
}
System.out.println("");
System.out.print("The value of Mclauren's Expansion of e^x = " + result);
}
}