//Using the below series here is the code to calculate the Sine value
NOTE- value of x (the argument of sine function) is given in degree or you can modify the code to accept the input in radian
public class sineFunction {
public static void main(String[] args) {
int numTerms = 4;
double degree = 20;
double argsinRadian;
argsinRadian = (degree * 3.14) / 180;
System.out.println("value of sin(" + argsinRadian + ") is -" + evaluateSine(argsinRadian, numTerms));
} //main
public static double evaluateSine(double argsinRadian, int numTerms) {
double functionValue = argsinRadian;
double value = argsinRadian;
double squareValue = argsinRadian;
squareValue = argsinRadian * argsinRadian;
double temp = 0;
int p = 3;
for (int t = 2; t <= numTerms; t++) {
temp = value * squareValue;
value = temp;
temp = temp / fact(p);
p = p + 2;
if (t % 2 == 0) {
temp = temp * -1;
}
functionValue = functionValue + temp;
}
return functionValue;
}
public static int fact(int num) {
int factorial = 1;
for (int k = 1; k <= num; k++) {
factorial = factorial * k;
}
return factorial;
}
}
No comments:
Post a Comment