# Java cos Function

The Java cos Function is one of the Java Math functions, which is to calculate the Trigonometry Cosine for the specified expression. Before we get int to Java cos program, let us see the mathematical formula behind the Trigonometry Cosine function is as shown below:

cos(x) = Length of the Adjacent Side / Length of the Hypotenuse

## Syntax of a Java cos Function

The basic syntax of the Math.cos in Java Programming language is as shown below.

```static double cos(double number); //Return Type is Double

// In order to use in program:
Math.cos(double number);```

Number: It can be a number or a valid numerical expression for which you want to find Cosine value.

• If the number argument is a positive or negative number, the Java Math.cos function will return the Cosine value.
• If the number argument is not a number, the Math.cos function will return NaN.

## How to use cos in Java?

The Math.cos function will return the value between -1 and 1. We use the Java cos Function to find the trigonometry Cosine values of both positive and negative values. And display the output using Java Math.cos function

```// Java Math.cos Function

package TrigonometricFunctions;

public class CosMethod {
public static void main(String[] args) {
double x = Math.cos(1.96 - 4.65 + 2.98);
System.out.println("Cosine value =  " + x);

System.out.println("\nCosine value of Positive Number = " + Math.cos(10.25));
System.out.println("Cosine value of Positive Number=  " + Math.cos(2.28));

System.out.println("\nCosine value of Negative Number =  " + Math.cos(-4.85));
System.out.println("Cosine value of Negative Number =  " + Math.cos(-7.95));

System.out.println("\nCosine value of Negative Number = " + Math.cos(m));
}
}```

First, we declared a variable x of type Double and used the Math.cos function directly on expression. Here, we used System.out.println statement to print the result as output.

```double x = Math.cos(1.96 - 4.65 + 2.98);
System.out.println("Cosine value =  " + x);```

Math.cos(1.96 – 4.65 + 2.98)

==> Math.cos (0.29) ==> 0.95

Next, we used the Math.cos Function directly on Positive double values.

```System.out.println("\nCosine value of Positive Number = " + Math.cos(10.25));
System.out.println("Cosine value of Positive Number=  " + Math.cos(2.28));```

Here, we used the Java cos Function directly on Negative double values.

```System.out.println("\nCosine value of Negative Number =  " + Math.cos(-4.85));
System.out.println("Cosine value of Negative Number =  " + Math.cos(-7.95));```

Next, we declared a variable of type Double and assigned the value. Next, we used the Math.toRadians function to convert 30 into equivalent radiant. Then the Java System.out.println statement to print the result as output.

```double m = Math.toRadians(30);
System.out.println("\nCosine value of Negative Number = " + Math.cos(m));```

### Java cos Array example

In this Java program, we find the Cosine values of bulk data. Here, we are going to declare an array of double type and find the Cosine values of array elements.

```package TrigonometricFunctions;

public class CosMethodOnArray {
public static void main(String[] args) {

double [] myArray = {0, 1, 30, 45, 60, 75, 90, 120, 180, 240, 360};

for (int i = 0; i < myArray.length; i++) {
System.out.format("Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.cos(myArray[i]));
}
}
}```
``````Cosine value of Array Element 0.00 = 1.0000
Cosine value of Array Element 1.00 = 0.5403
Cosine value of Array Element 30.00 = 0.1543
Cosine value of Array Element 45.00 = 0.5253
Cosine value of Array Element 60.00 = -0.9524
Cosine value of Array Element 75.00 = 0.9218
Cosine value of Array Element 90.00 = -0.4481
Cosine value of Array Element 120.00 = 0.8142
Cosine value of Array Element 180.00 = -0.5985
Cosine value of Array Element 240.00 = 0.3258
Cosine value of Array Element 360.00 = -0.2837``````

We used the Java For Loop to iterate the Array. Within the Cosine For Loop, we initialized the i value as 0. Next, the compiler executes as long as the condition (i < myArray.length) is true.

TIP: myArray.length finds the length of an array.

`for (int i = 0; i < myArray.length; i++) {`

Here, we used the Java cos Function directly inside the System.out.format statement. Here, it will call the Math.cos method ( static double cos(double number) ) to find the corresponding Cosine values and prints the output.

`System.out.format("Cosine value of Array Element %.2f = %.4f\n", myArray[i], Math.cos(myArray[i]));`

NOTE: To find the Cosine value of a single item, then use: Math.cos(myArray[index_position])

### How to use cos on Java Arraylist

In this Java program, we are going to declare an ArrayList of double type and find the Cosine values of list elements.

```package TrigonometricFunctions;

import java.util.ArrayList;

public class CosMethodOnArrayList {
public static void main(String[] args) {

ArrayList<Double> myList = new ArrayList<Double>(5);

for (double x : myList) {
System.out.format("Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.cos(x));
}
}
}```
``````Cosine value of ArrayList Item 15.45 =  -0.9669
Cosine value of ArrayList Item 65.64 =  -0.9449
Cosine value of ArrayList Item 95.60 =  0.2168
Cosine value of ArrayList Item 120.75 =  0.2000
Cosine value of ArrayList Item 140.68 =  -0.7702
Cosine value of ArrayList Item 180.98 =  0.3320 ``````

Within this math cos function example, we used the For Loop to iterate the double values in ArrayList

`for (double x : myList) {`

Here, the compiler will call the cos Math function ( static double cos(double x) ) to find the corresponding Cosine values, and print the output.

`System.out.format("Cosine value of ArrayList Item %.2f =  %.4f \n", x, Math.cos(x));`

TIP: Please refer to Java acos Function to find the Arc Cosine of the specified expression.