Trig in java: In the previous article, we have seen Java Program to Check if a Given Number is Fibonacci Number or Not
In this article we are going to see how Java Program to Find Trigonometric values of an Angel using Java programming language.
Java Program to Find Trigonometric Values of an Angel
Trig functions in java: Before Jumping into the program directly let’s see how Java Program to Find Trigonometric values of an Angel.
Explanation:
Java has math package i.e. java.lang.Math
which contains all the trigonometric functions.
We can find trigonometric values like sin, cos, tan, sec, cosec and cot of an angle using sin(), cos() and tan() methods of Math class. Like below
- sine of an angle : Math.sin();
- cosine of an angle : Math.cos();
- tangent of an angle : Math.tan();
- sec of an angle : 1/Math.cos();
- cosec of an angle : 1/Math.sin();
- cot of an angle : 1/Math.tan();
Example:
Let angle “a” = 60 Sin 60 = √3/2 Cos 60 = 1/2 Tan 60 = sin60/cos60 = √3 Sec 60 = 1/cos60 = 2 Cosec 60 = 1/sin60 = 2/√3 Cot 60 = 1/tan60 = 1/√3
Let’s see different ways to find trigonometric values of an angle.
Method-1: Java Program to Find Trigonometric Values of an Angel By Using Static Input Value
Approach:
- Declare a double variable say ‘
a
’ and assign the angle value to it. - Now convert that number into angle using
toRadians(angle)
method of math class. - Now find out trigonometric values of an angle using methods of Math class.
- Print the result.
Program:
import java.lang.Math; class Main { public static void main(String [] args) { //angle as double value double a = 60; //converting angle to radians. double r = Math.toRadians(a); //finding the trigonometric values double sin = Math.sin(r); double cos = Math.cos(r); double tan = Math.tan(r); double sec = (1 / cos); double cosec = (1 / sin); double cot = (1 / tan); System.out.println("sin("+ a + ")=" + sin); System.out.println("cos(" + a + ")=" + cos); System.out.println("tan(" + a + ")=" + tan); System.out.println("sec(" + a + ")=" + sec); System.out.println("cosec(" + a + ")=" + cosec); System.out.println("cot(" + a + ")=" + cot); } }
Output: sin(60.0)=0.8660254037844386 cos(60.0)=0.5000000000000001 tan(60.0)=1.7320508075688767 sec(60.0)=1.9999999999999996 cosec(60.0)=1.1547005383792517 cot(60.0)=0.577350269189626
Method-2: Java Program to Find Trigonometric Values of an Angel By Using User Input Value
Approach:
- Declare a double variable say ‘
a
’ and take the angle value as input from user. - Now convert that number into angle using
toRadians(angle)
method of math class. - Now find out trigonometric values of an angle using methods of Math class.
- Print the result.
Program:
import java.lang.Math; import java.util.Scanner; class Main { public static void main(String [] args) { Scanner s = new Scanner(System.in); //Taking angle value input from user System.out.println("Enter angle to find its all trigonometric values:"); //angle as double value double a = s.nextDouble(); //converting angle to radians. double r = Math.toRadians(a); //finding the trigonometric values double sin = Math.sin(r); double cos = Math.cos(r); double tan = Math.tan(r); double sec = (1 / cos); double cosec = (1 / sin); double cot = (1 / tan); System.out.println("sin("+ a + ")=" + sin); System.out.println("cos(" + a + ")=" + cos); System.out.println("tan(" + a + ")=" + tan); System.out.println("sec(" + a + ")=" + sec); System.out.println("cosec(" + a + ")=" + cosec); System.out.println("cot(" + a + ")=" + cot); } }
Output: Case-1 Enter angle to find its all trigonometric values: 90 sin(90.0)=1.0 cos(90.0)=6.123233995736766E-17 tan(90.0)=1.633123935319537E16 sec(90.0)=1.633123935319537E16 cosec(90.0)=1.0 cot(90.0)=6.123233995736766E-17 Case-2 Enter angle to find its all trigonometric values: 45 sin(45.0)=0.7071067811865475 cos(45.0)=0.7071067811865476 tan(45.0)=0.9999999999999999 sec(45.0)=1.414213562373095 cosec(45.0)=1.4142135623730951 cot(45.0)=1.0000000000000002
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: