Write a Java program to Find the trigonometric cosine of an angle
- import java.util.*; - This line imports the java.util package, which includes the Scanner class that we will use to read user input.
- public class Trigonometric_Cosine - This line defines a public class called Trigonometric_Cosine, which contains the code for our program.
- public static void main(String[] args) - This line defines a public static method called main, which is the entry point of our program. This method takes an array of String objects as an argument.
- Scanner input = new Scanner(System.in); - This line creates a new Scanner object called input, which we will use to read user input from the command line.
- double deg = 0; - This line declares a variable called deg of type double and initializes it to 0.
- System.out.print("Enter The Degree : "); - This line prints the string "Enter The Degree : " to the command line.
- deg = input.nextDouble(); - This line reads a double value from the command line using the Scanner object input and stores it in the variable deg.
- double rad = Math.toRadians(deg); - This line converts the value of deg from degrees to radians using the Math.toRadians method and stores it in a variable called rad.
- System.out.print("Trigonometric Cosine is : " + Math.cos(rad)); - This line calculates the cosine of rad using the Math.cos method and prints it to the command line along with the string "Trigonometric Cosine is : ".
Source Code
import java.util.*;
public class Trigonometric_Cosine
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double deg = 0;
System.out.print("Enter The Degree : ");
deg = input.nextDouble();
double rad = Math.toRadians(deg);
System.out.print("Trigonometric Cosine is : " + Math.cos(rad));
}
}
Output
Enter The Degree : 160
Trigonometric Cosine is : -0.93969262078590