C# | Math.Cos() Method

Math.Cos() is an inbuilt Math class method which returns the cosine of a given double value argument(specified angle).

Syntax:

public static double Cos(double num)

Parameter:

num: It is the angle(measured in radian) whose cosine is to be returned and the type of this parameter is System.Double.

Return Value: Returns the csoine of num of type System.Double. If num is equal to NegativeInfinity, PositiveInfinity, or NaN, then this method returns NaN.



Below are the programs to illustrate the Math.Cos() method.

Program 1: To show the working of Math.Cos() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate working
// Math.Cos() method
using System;
   
class Geeks {
   
    // Main Method
    public static void Main(String []args)
    {
        double a = 70;
           
        // converting value to radians
        double b = (a * (Math.PI)) / 180;
   
        // using method and displaying result
        Console.WriteLine(Math.Cos(b));
        a = 50;
           
        // converting value to radians
        b = (a * (Math.PI)) / 180;
   
        // using method and displaying result
        Console.WriteLine(Math.Cos(b));
        a = 73;
           
        // converting value to radians
        b = (a * (Math.PI)) / 180;
  
        // using method and displaying result
        Console.WriteLine(Math.Cos(b));
        a = 77;
           
        // converting value to radians
        b = (a * (Math.PI)) / 180;
   
        // using method and displaying result
        Console.WriteLine(Math.Cos(b));
    }
}

chevron_right


Output:

0.342020143325669
0.642787609686539
0.292371704722737
0.224951054343865

 

Program 2: To show the working of Math.Cos() method when the argument is NaN or infinity.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate working
// Math.Cos() method in infinity case
using System;
  
class Geeks {
      
    // Main Method
    public static void Main(String []args)
    {
  
        double positiveInfinity = Double.PositiveInfinity;     
        double negativeInfinity = Double.NegativeInfinity;
                 
                 
        double nan = Double.NaN;
        double result;
  
        // Here argument is negative infinity,
        // output will be NaN
         result = Math.Cos(negativeInfinity);
         Console.WriteLine(result);
  
        // Here argument is positive infinity,
        // output will also be NaN
        result = Math.Cos(positiveInfinity);
        Console.WriteLine(result);
  
        // Here argument is NaN, output will be NaN
        result = Math.Cos(nan);
        Console.WriteLine(result);
    }
}

chevron_right


Output:

NaN
NaN
NaN


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.