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 into to Java cos program, let us see the mathematical formula behind the Trigonometry Cosine function as shown below:

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

Java cos Function Syntax

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 the Cosine value.

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

How to use cos in Java?

This function will return a value between -1 and 1. We use the Java math cos Function to find the trigonometry Cosine values of both positive and negative values. And display the output using this 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));

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

First, we declared a variable x of type Double and used the Java Math.cos function directly on the 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 an equivalent radiant. Then the Java System.out.println statement prints 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 will 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 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 cos Function directly inside the System.out.format statement. Here, it will call the Math 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 will 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);
		myList.add(15.45);
		myList.add(65.64);
		myList.add(95.60);
		myList.add(120.75);
		myList.add(140.68);
		myList.add(180.98);
		
		for (double x : myList) {
			System.out.format("Cosine of %.2f =  %.4f \n", x, Math.cos(x));
		}
	}
}
Cosine of 15.45 =  -0.9669 
Cosine of 65.64 =  -0.9449 
Cosine of 95.60 =  0.2168 
Cosine of 120.75 =  0.2000 
Cosine of 140.68 =  -0.7702 
Cosine of 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) {

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 acos Function to find the Arc Cosine of the specified expression.