3 prg
import java.io.;
import java.util.;
public class MathFun {
public void sin1(double x1) {
int i = 1, j = 3;
double sum1 = 0.0, sum2 = 0.0;
while (i <= 20) {
double f = Math.pow(x1, i) / fact(i);
sum1 += f;
i += 4;
}
while (j <= 20) {
double f = Math.pow(x1, j) / fact(j);
sum2 += f;
j += 4;
}
System.out.println("MATH SIN X: " + Math.sin(x1));
System.out.println("SIN X: " + (sum1 - sum2));
System.out.println("RELATIVE ERROR: " + (Math.sin(x1) - (sum1 - sum2)));
}
public void cos1(double x1) {
int i = 4, j = 2;
double sum1 = 0.0, sum2 = 0.0;
while (i < 20) {
double f = Math.pow(x1, i) / fact(i);
sum1 += f;
i += 4;
}
while (j <= 20) {
double f = Math.pow(x1, j) / fact(j);
sum2 += f;
j += 4;
}
System.out.println("MATH COS X: " + Math.cos(x1));
System.out.println("COS X: " + (1 + sum1 - sum2));
System.out.println("RELATIVE ERROR: " + (Math.cos(x1) - (1 + sum1 - sum2)));
}
public void ex1(double x1) {
int i = 1;
double sum1 = 0.0;
while (i < 10) {
double f = Math.pow(x1, i) / fact(i);
sum1 += f;
i++;
}
double f1 = Math.pow(Math.E, x1);
double f2 = (1 + sum1);
System.out.println("MATH E POWER X: " + f1);
System.out.println("MY E POWER X: " + f2);
System.out.println("RELATIVE ERROR: " + (f1 - f2));
}
// Factorial function
public int fact(int g) {
if (g <= 1) {
return 1;
} else {
return g * fact(g - 1);
}
}
}
class MathFunMain {
public static void main(String args[]) throws IOException {
MathFun mathFun = new MathFun();
DataInputStream in = new DataInputStream(System.in);
System.out.println("ENTER THE VALUE IN DEGREES:");
double x = Double.parseDouble(in.readLine());
double x2 = (x * Math.PI) / 180; // Convert degrees to radians
System.out.println("******************************");
System.out.println("SIN X:");
System.out.println("******************************");
mathFun.sin1(x2);
System.out.println("******************************");
System.out.println("COS X:");
System.out.println("******************************");
mathFun.cos1(x2);
System.out.println("******************************");
System.out.println("E POWER X:");
System.out.println("******************************");
mathFun.ex1(x2);
}
}