Java sin Function

The Java sin function is one of the Math functions, which is to calculate the Trigonometry Sine for the specified expression. The mathematical formula behind the Trigonometry Sine function is as shown below:

SIN(x) = Length of the Opposite Side / Length of the Hypotenuse

Java sin Syntax

The basic syntax of the sin Function in Java Programming language is shown below.

static double sin(double number); //Return Type is Double

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

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

  • If the number argument is a positive or negative number, the Java sin function will return the Sine value.
  • If the number argument is not a number or Infinity, the function will return NaN.

Java sin Function Example

This function will return a value between -1 and 1. In this program, we use the Java Math.sin Function to find the trigonometric Sine values of both positive and negative values and display the output

TIP: Please refer to the ASIN Function article to find the Java Arc Sine of the specified expression.

package TrigonometricFunctions;

public class SinMethod {
	public static void main(String[] args) {
		double x = Math.sin(1.96 - 4.65 + 2.98);
		System.out.println("Sine value =  " + x);	
		
		System.out.println("\nSine value of Positive Number = " + Math.sin(10.25));
		System.out.println("Sine value of Positive Number=  " + Math.sin(2.28));
		
		System.out.println("\nSine value of Negative Number =  " + Math.sin(-4.85));
		System.out.println("Sine value of Negative Number =  " + Math.sin(-7.95));

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

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

double x = Math.sin(1.96 - 4.65 + 2.98);
System.out.println("Sine value =  " + x);

Math.sin(1.96 – 4.65 + 2.98)

==> (0.29) ==> 0.28

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

We used the Java sin Function directly on Negative double values in the next two statements.

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

Java sin on Array example

In this Java program, we find the sine values of bulk data. Here, we will declare an array of double types and find the sine values of array elements.

package TrigonometricFunctions;

public class SinMethodOnArray {
	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("Sine value of %.2f = %.4f\n", myArray[i], Math.sin(myArray[i]));
		}
	}
}
Sine value of 0.00 = 0.0000
Sine value of 1.00 = 0.8415
Sine value of 30.00 = -0.9880
Sine value of 45.00 = 0.8509
Sine value of 60.00 = -0.3048
Sine value of 75.00 = -0.3878
Sine value of 90.00 = 0.8940
Sine value of 120.00 = 0.5806
Sine value of 180.00 = -0.8012
Sine value of 240.00 = 0.9454
Sine value of 360.00 = 0.9589

We used Java For Loop to iterate the Array. Within the Sine For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length).

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

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

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

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

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

Java sin Function on Arraylist example

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

Within this example, we used the For Loop to iterate the double values in ArrayList.

The compiler will call the math method ( static double sin(double x) ) to find the corresponding Sine values and print the output.

package TrigonometricFunctions;

import java.util.ArrayList;

public class SinMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(30.45);
		myList.add(45.64);
		myList.add(120.60);
		myList.add(180.75);
		myList.add(90.68);
		myList.add(14.98);
		
		for (double x : myList) {
			System.out.format("%.2f =  %.4f \n", x, Math.sin(x));
		}
	}
}
30.45 =  -0.8226 
45.64 =  0.9962 
120.60 =  0.9389 
180.75 =  -0.9941 
90.68 =  0.4134 
14.98 =  0.6654