-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sincos.java
91 lines (74 loc) · 2.1 KB
/
Sincos.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
public class Sincos{
static final double PI = 3.142;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of x:(angle) ");
double x = sc.nextDouble();
//convert the angle into radians
x = x * (PI / 180.0);
System.out.print("Enter the value of n: ");
double n = sc.nextDouble();
double cosine= cosX(x, n);
double sine = sinX(x,n);
System.out.println("Enter the values of p and q for power");
double p = sc.nextDouble();
double q = sc.nextDouble();
double pow = power(p, q);
System.out.println("Enter the value of f for factorial");
double f = sc.nextDouble();
double fact = fact(f);
sc.close();
System.out.println("The sinx value is"+ sine);
System.out.println("The cosx value is"+ cosine);
System.out.println(pow);
System.out.println(fact);
}
static double cosX(double x, double n)
{
double ans=0;
int c=0;
//this 'for loop' is the Maclaurin expansion of cosx
for (int i = 1; i < n; i++)
{
if(i%2==1)
ans= ans+ (power(x,c)/fact(c));
else
ans= ans- (power(x,c)/fact(c));
c= c+2;
}
return ans;
}
static double sinX(double x, double n)
{
double ans=0;
int c=1;
//this 'for loop' is the Maclaurin expansion of sinx
for (int i = 1; i < n; i++)
{
if(i%2==1)
ans= ans+ (power(x,c)/fact(c));
else
ans= ans- (power(x,c)/fact(c));
c= c+2;
}
return ans;
}
static double fact(double c)
{
double factorial=1;
for(int i = 1; i <= c; ++i)
{
factorial *= i;
}
return factorial;
}
static double power(double x, double c)
{
double power=1;
for (int i = 1; i <= c; i++)
power = power * x;
return power;
}
}