MIT-Curricular/OOP/Java/Lab/Week3/SqInvAndSinCalc.java

40 lines
1.1 KiB
Java
Raw Permalink Normal View History

2024-08-10 11:20:17 +05:30
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();
2024-08-10 11:23:06 +05:30
double x = y * (3.1415926 / 180.0);
sinx = x;
2024-08-10 11:20:17 +05:30
int n = 1;
2024-08-10 11:23:06 +05:30
2024-08-10 11:20:17 +05:30
for (int i = 1; i <= terms; i++) {
2024-08-10 11:23:06 +05:30
double term = Math.pow(-1, n) * Math.pow(x, 2 * n + 1) / factorial(2 * n + 1);
2024-08-10 11:20:17 +05:30
sinx += term;
n++;
}
2024-08-10 11:23:06 +05:30
System.out.println("The value of Sin(" + x + ") is: " + sinx);
System.out.println("The sum is " + sum);
}
2024-08-10 11:20:17 +05:30
2024-08-10 11:23:06 +05:30
public static double factorial(int n) {
double result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
2024-08-10 11:20:17 +05:30
}
}