Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# | Math.Cos() Method

  • Last Updated : 31 Jan, 2019

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.




// 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));
    }
}
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.




// 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);
    }
}
Output:
NaN
NaN
NaN



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!