Java .cos()

THE-Spellchecker's avatar
Published Jan 18, 2024Updated May 15, 2024
Contribute to Docs

The .cos() method returns the trigonometric cosine of the given angle. The result is in the range [-1, 1].

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.cos(angle)
  • angle: This is the only parameter the .cos() method takes and is expressed in radians.

Note: If the angle is NaN or Infinity, NaN is returned.

Example

The following example demonstrates the application of .cos() method:

public class Check {
public static void main(String args[]) {
// convert degrees to radians
double pi = Math.PI;
double degree = 60;
double radian = degree * pi/180;
//to get output up to the first decimal place
double roundedResult = Math.round(Math.cos(radian) * 10.0) / 10.0;
System.out.println("Cosine of 60 degrees is " + roundedResult);
}
}

The above example results in the following output:

Cosine of 60 degrees is 0.5

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours