Java sin Function

The Java sin function is one of the Java 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

Syntax of a Java sin Function

The basic syntax of the sin Function in Java Programming language is as 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 Sine value. Remember, Number should be in Radians or an Angle

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

Java sin Function Example

The Java sin function will return the value between -1 and 1. In this Java 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 Java ASIN Function article to find the Java Arc Sine of the specified expression.

// Java sin Function

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 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)

==> Math.sin (0.29) ==> 0.28

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

System.out.println("\nSine value of Positive Number = " + Math.sin(10.25));
System.out.println("Sine value of Positive Number=  " + Math.sin(2.28));

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

System.out.println("\nSine value of Negative Number =  " + Math.sin(-4.85));
System.out.println("Sine value of Negative Number =  " + Math.sin(-7.95));

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

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

Java sin on Array example

In this Java program, we find the sine values of bulk data. Here, we are going to declare an array of double type 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 Array Element %.2f = %.4f\n", myArray[i], Math.sin(myArray[i]));
		}
	}
}
Sine value of Array Element 0.00 = 0.0000
Sine value of Array Element 1.00 = 0.8415
Sine value of Array Element 30.00 = -0.9880
Sine value of Array Element 45.00 = 0.8509
Sine value of Array Element 60.00 = -0.3048
Sine value of Array Element 75.00 = -0.3878
Sine value of Array Element 90.00 = 0.8940
Sine value of Array Element 120.00 = 0.5806
Sine value of Array Element 180.00 = -0.8012
Sine value of Array Element 240.00 = 0.9454
Sine value of Array Element 360.00 = 0.9589

We used the 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++) {

Here, we used the Math.sin Function directly inside the System.out.format statement. Here, it will call the Math.sin 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.

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("Sine value of ArrayList Item %.2f =  %.4f \n", x, Math.sin(x));
		}
	}
}
Sine value of ArrayList Item 30.45 =  -0.8226 
Sine value of ArrayList Item 45.64 =  0.9962 
Sine value of ArrayList Item 120.60 =  0.9389 
Sine value of ArrayList Item 180.75 =  -0.9941 
Sine value of ArrayList Item 90.68 =  0.4134 
Sine value of ArrayList Item 14.98 =  0.6654 

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

for (double x : myList) {

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

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