Close

Java - Math.sin() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Method:

public static double sin(double a)

Returns the trigonometric sine of an angle.


Examples


package com.logicbig.example.math;

public class SinExample {

public static void main(String... args) {
findSine(0);
findSine(30);
findSine(45);
findSine(60);
findSine(90);
}

private static void findSine(double degrees) {
double radians = Math.toRadians(degrees);
double sinValue = Math.sin(radians);
System.out.printf("Sine of %s degree = %.2f %n", degrees, sinValue);
}
}

Output

Sine of 0.0 degree = 0.00 
Sine of 30.0 degree = 0.50
Sine of 45.0 degree = 0.71
Sine of 60.0 degree = 0.87
Sine of 90.0 degree = 1.00




package com.logicbig.example.math;

public class SinGraphExample {

public static void main(String... args) {

int scale = 10;
for (double i = 0; i <= Math.PI * 2; i += 0.3) {
double sin = Math.sin(i);
int sinValue = (int) Math.round(sin * scale);
if (sinValue < 0) {
int spaces = scale - Math.abs(sinValue);
System.out.print(" ".repeat(spaces) + "+");
System.out.println(" ".repeat(scale - spaces - 1) + "|");

} else {
System.out.print(" ".repeat(scale) + "|");
System.out.println(" ".repeat(sinValue) + "+");
}
}
}
}

Output

          |+
| +
| +
| +
| +
| +
| +
| +
| +
| +
| +
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
+ |




See Also