Java Program to Implement the sin() Function (approximately)

«
»

This is the Java Program to Implement the sin() Function (approximately).

Problem Description

Given an angle say x, in degrees find out the sine of the angle approximately.

Problem Solution

The sine of an angle x can be calculated using the following equation

advertisement

sin x = x – (x3)/3! + (x5)/5! – (x7)/7! + ….. = Summation ((-1)n* x(2n+1)/(2n+1)!) for n = 0 to n = infinity.

Note: In the series, x is in radians.

Program/Source Code

Here is the source code of the Java Program to Implement the cos() Function(approximately). The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
  1.  
  2. // Java Program to Implement the cos() Function(approximately)
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Sine {
  8.     // Function to calculate and display sine of an angle
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double x;
  12.         try {
  13.             System.out.println("Enter the angle whose sine is to be
  14.                                              calculated in degrees");
  15.             x = Double.parseDouble(br.readLine());
  16.         } catch (Exception e) {
  17.             System.out.println("An error occurred");
  18.             return;
  19.         }
  20.         double y;
  21.         y = x*Math.PI/180;
  22.         int n = 10;
  23.         int i,j,fac;
  24.         double sine = 0;
  25.         for(i=0; i<=n; i++){
  26.             fac = 1;
  27.             for(j=2; j<=2*i+1; j++){
  28.                 fac*=j;
  29.             }
  30.             sine+=Math.pow(-1.0,i)*Math.pow(y,2*i+1)/fac;
  31.         }
  32.         System.out.format("The sine of " + x + " is %f",sine);
  33.     }
  34. }
Program Explanation

1. In function main(), firstly the angle is entered in degrees and it is converted into radians.
2. The loop for(i=0; i<=n; i++) is used to calculate the ith term of the series.
3. The nested loop for(j=2; j<=2*i+1; j++) is used to calculate the factorial of 2i+1.
4. Finally, the ith term is added to the variable sine.

Time Complexity: O(1).

advertisement
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the angle whose sine is to be calculated in degrees
30
The sine of 30.0 is 0.500000
 
Case 2 (Simple Test Case - another example):
 
Enter the angle whose sine is to be calculated in degrees
60
The sine of 60.0 is 0.866025

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn