Java Program to Implement the cos() Function

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

Problem Description

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

Problem Solution

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

cos x = 1 – (x2)/2! + (x4)/4! – (x6)/6! + ….. = Summation ((-1)n * x(2n)/(2n)!) 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
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 Cosine {
  8.     // Function to read user input and calculate the cosine of the 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 cosine 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 cosine = 0;
  25.         for(i=0; i<=n; i++){
  26.             fac = 1;
  27.             for(j=2; j<=2*i; j++){
  28.                 fac*=j;
  29.             }
  30.             cosine+=Math.pow(-1.0,i)*Math.pow(y,2*i)/fac;
  31.         }
  32.         System.out.format("The cosine of " + x + " is %f",cosine);
  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; j++) is used to calculate the factorial of 2i.
4. Finally, the ith term is added to the variable cosine.

Time Complexity: O(1).

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the angle whose cosine is to be calculated in degrees
45
The cosine of 45.0 is 0.707107
 
Case 2 (Simple Test Case - another example):
 
Enter the angle whose cosine is to be calculated in degrees
75
The cosine of 75.0 is 0.258819

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.