# Java tan Function

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

TAN(x) = Length of the Opposite Side / Length of the Adjacent Side

## Java tan Function Syntax

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

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

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

Number: It can be a number or a valid numerical expression for which you want to find Tangent value. Remember, Number should be in Radians or an Angle

• If the number argument is a positive or negative number, the Java Math.tan function will return the Tangent value.
• If the number argument is not a number or Infinity, the Math.tan function will return NaN.
• When the number argument is Zero, the Math.tan function will return Zero with the same sign.

## Java tan Function Example

In this Java program, we use the Java Math.tan Function to find the trigonometric Tangent values of both positive and negative values and display the output.

TIP: Please refer to the Java atan Function article to find the Java trigonometric Arc Tangent of the specified expression.

```// Java Math.tan Function

package TrigonometricFunctions;

public class TanMethod {
public static void main(String[] args) {
double x = Math.tan(4.96 + 24.65 - 6.98);
System.out.println("Tangent value =  " + x);

System.out.println("\nTangent value of Positive Number = " + Math.tan(14.95));
System.out.println("Tangent value of Positive Number=  " + Math.tan(12.28));

System.out.println("\nTangent value of Negative Number =  " + Math.tan(-10.85));
System.out.println("Tangent value of Negative Number =  " + Math.tan(-9.35));

System.out.println("\nTangent value of Radians = " + Math.tan(m));
}
}```

Within this Java tan function example, we declared variable x of type Double. We used the Java Math.tan function directly on expression. Here, we used System.out.println statement to print the tangent result as output.

```double x = Math.tan(4.96 + 24.65 - 6.98);
System.out.println("Tangent value =  " + x);```

Math.tan(4.96 + 24.65 – 6.98)

==> Math.tan (22.63) ==> 0.7427600642569245

Next, we used the tan Math function directly on Positive double values.

```System.out.println("\nTangent value of Positive Number = " + Math.tan(14.95));
System.out.println("Tangent value of Positive Number=  " + Math.tan(12.28));```

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

```System.out.println("\nTangent value of Negative Number =  " + Math.tan(-10.85));
System.out.println("Tangent value of Negative Number =  " + Math.tan(-9.35));```

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

```double m = Math.toRadians(90);
System.out.println("\nTangent value of Radians = " + Math.tan(m));```

## Java tan on Array example

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

```package TrigonometricFunctions;

public class TanMethodOnArray {
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("Tangent value of Array Element %.2f = %.4f\n", myArray[i], Math.tan(myArray[i]));
}
}
}```
``````Tangent value of Array Element 0.00 = 0.0000
Tangent value of Array Element 1.00 = 1.5574
Tangent value of Array Element 30.00 = -6.4053
Tangent value of Array Element 45.00 = 1.6198
Tangent value of Array Element 60.00 = 0.3200
Tangent value of Array Element 75.00 = -0.4207
Tangent value of Array Element 90.00 = -1.9952
Tangent value of Array Element 120.00 = 0.7131
Tangent value of Array Element 180.00 = 1.3387
Tangent value of Array Element 240.00 = 2.9021
Tangent value of Array Element 360.00 = -3.3801``````

We used the Java For Loop to iterate the Array. Within the tan For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length). The statement inside the for loop executed until the condition is False.

TIP: myArray.length is used to find the length of an array.

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

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

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

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

## Java tan Function on Arraylist example

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

```package TrigonometricFunctions;

import java.util.ArrayList;

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

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

for (double x : myList) {
System.out.format("Tangent value of ArrayList Item %.2f =  %.4f \n", x, Math.tan(x));
}
}
}```
``````Tangent value of ArrayList Item 3.14 =  -0.0000
Tangent value of ArrayList Item 1.05 =  1.7321
Tangent value of ArrayList Item 0.79 =  1.0000
Tangent value of ArrayList Item 0.52 =  0.5774
Tangent value of ArrayList Item 0.35 =  0.3640 ``````

First, We declared an ArrayList of double type and assigned some random value using Math.PI constant (its value is approximately 3.14)

```ArrayList<Double> myList = new ArrayList<Double>(5);
`for (double x : myList) {`
`System.out.format("Tangent value of ArrayList Item %.2f =  %.4f \n", x, Math.tan(x));`