40 lines
1.1 KiB
Java
40 lines
1.1 KiB
Java
import java.util.Scanner;
|
|
public class SqInvAndSinCalc {
|
|
public static void main(String args[]){
|
|
Scanner sc = new Scanner(System.in);
|
|
double sum=0, sinx=0;
|
|
|
|
System.out.println("Enter the number of terms you want to check (for both Sine and Sum):");
|
|
int terms = sc.nextInt();
|
|
|
|
for(int i=1; i<=terms ; i++){
|
|
sum += Math.pow(i,-i);
|
|
}
|
|
|
|
System.out.println("Enter the input in degrees for the Sine function:");
|
|
double y = sc.nextDouble();
|
|
|
|
double x = y * (3.1415926 / 180.0);
|
|
sinx = x;
|
|
int n = 1;
|
|
|
|
for (int i = 1; i <= terms; i++) {
|
|
double term = Math.pow(-1, n) * Math.pow(x, 2 * n + 1) / factorial(2 * n + 1);
|
|
sinx += term;
|
|
n++;
|
|
}
|
|
|
|
System.out.println("The value of Sin(" + x + ") is: " + sinx);
|
|
|
|
System.out.println("The sum is " + sum);
|
|
}
|
|
|
|
public static double factorial(int n) {
|
|
double result = 1;
|
|
for (int i = 2; i <= n; i++) {
|
|
result *= i;
|
|
}
|
|
return result;
|
|
}
|
|
}
|