Hi, I think I'm finally almost done with this program. I can get my result somewhat close but I think I know what's the problem. The purpose of the program is to compute the Sine function w/o Math.sin().
MaxE is a user input of the maximum exponent that the series will go up to
Rad is the user input for the radians
When I input 30 (degrees), the sine should be .49999999... or roughly .5 but after the calculations, the program out puts 0.47827350788327533. What I think the program is doing is calculating the factorials as
1! = 1
3! = 1 * 3
5! = 1 * 3 * 5
7! = 1 * 3 * 5 * 7
etc..
instead of doing a normal factorial
1! = 1
3! = 1 * 2 * 3
5! = 1 * 2 * 3 * 4 * 5
etc..
I was just curious if my thought process was correct, if so, I was wondering if any could give me a hint as to what I should do to fix this.
*edit* By the way, my idea for solving this problem was trying to create another factorial but instead of finding the odd numbers, rewrite another line to do even numbers and the multiply the two, would this work?
here's the whole code if you were wondering why I said 30 degrees
import java.io.*; public class d { public static void main(String args[]) throws IOException { BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in)); String s; double Rad, Sine; int MaxE, fact; Rad = 0.0; Sine = 0.0; fact = 1; System.out.println("Degrees (d) or Radians (r)? Enter (d) or (r)" ); s = keybd.readLine(); s = s.toLowerCase(); s = s.trim(); if (s.equals("d")) { System.out.println("Enter a value (Degrees): "); Rad = Double.parseDouble(keybd.readLine()); Rad = (Math.PI * Rad) / 180; } else if (s.equals("r")) { System.out.println("Enter a value (Radians): "); Rad = Double.parseDouble(keybd.readLine()); } System.out.println("Enter the desired maximum exponent: "); MaxE = Integer.parseInt(keybd.readLine()); for (int i = 0; i <= MaxE; i++) { fact = fact*(2*i+1); Sine += Math.pow(-1,i)*Math.pow(Rad,2*i+1)/fact; } System.out.println("The value of Sine is " + Sine); System.out.println("Check: Sine is " + Math.sin(Rad)); } }
Thank you and any help is appreciated! Also, I apologize if this isn't the right forum for this topic