Open In App

Trigonometric Functions in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

  1. Java.lang.Math.sin() Method : is an inbuilt method which returns the sine of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument.

    Syntax :

    Math.sin(double radians)

    Parameters :
    The method takes one mandatory argument in radians.

    Returns :
    It returns a double value. The returned value is the sine of the specified double value passed.

    Example 1 : Program demonstrating the use of sin()




    // Java program for sin() method
    import java.util.*;
      
    class GFG {
      
        // Driver Code
        public static void main(String args[])
        {
      
            double degrees = 45.0;
      
            // convert degrees to radians
            double radians = Math.toRadians(degrees);
      
            // sin() method to get the sine value
            double sinValue = Math.sin(radians);
      
            // prints the sine value
            System.out.println("sin(" + degrees + ") = " + sinValue);
        }
    }

    
    

    Output:

    sin(45.0) = 0.7071067811865475
    
  2. Java.lang.Math.cos() : is an inbuilt method which returns the cosine of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN.

    Syntax :

    Math.cos(double radians)

    Parameters :
    The method takes one mandatory argument in radians.

    Returns :
    It returns a double value. The returned value is the cosine of the specified double value passed.

    Example 2 : Program demonstrating the use of cos()




    // Java program for cos() method
    import java.util.*;
      
    class GFG {
      
        // Driver Code
        public static void main(String args[])
        {
      
            double degrees = 45.0;
      
            // convert degrees to radians
            double radians = Math.toRadians(degrees);
      
            // cos() method to get the cosine value
            double cosValue = Math.cos(radians);
      
            // prints the cosine value
            System.out.println("cos(" + degrees + ") = " + cosValue);
        }
    }

    
    

    Output:

    cos(45.0) = 0.7071067811865476
    
  3. Java.lang.Math.tan() : is an inbuilt method which returns the tangent of the value passed as an argument. The value passed in this function should be in radians. If the argument is NaN or an infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument.
    Syntax :

    Math.tan(double radians)

    Parameters :
    The method takes one mandatory argument in radians.

    Returns :
    It returns a double value. The returned value is the tangent of the specified double value passed.

    Example 3 : Program demonstrating the use of tan()




    // Java program for tan() method
    import java.util.*;
      
    class GFG {
      
        // Driver Code
        public static void main(String args[])
        {
      
            double degrees = 45.0;
      
            // convert degrees to radians
            double radians = Math.toRadians(degrees);
      
            // cos() method to get the tangent value
            double tanValue = Math.tan(radians);
      
            // prints the tangent value
            System.out.println("tan(" + degrees + ") = " + tanValue);
        }
    }

    
    

    Output:

    tan(45.0) = 0.9999999999999999
    


Last Updated : 27 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads