Math.Cos() Method in C#

The Math.Cos() method in C# returns the cosine of a specified angle. The angle must be provided in radians, not degrees. This method is part of the System.Math class and is commonly used in mathematical calculations, graphics programming, and trigonometric operations.

Syntax

Following is the syntax for the Math.Cos() method −

public static double Cos(double val);

Parameters

  • val − A double value representing the angle in radians whose cosine is to be calculated.

Return Value

The method returns a double value representing the cosine of the specified angle. The return value ranges from -1 to 1.

Cosine Function Range 1 60° 0.5 90° 0 120° -0.5 180° -1

Using Math.Cos() with Degree Conversion

Since Math.Cos() expects radians, you need to convert degrees to radians using the formula: radians = degrees × ? / 180

Example

using System;

public class Demo {
    public static void Main() {
        double val1 = 30.0;
        double radians = (val1 * Math.PI) / 180;
        Console.WriteLine("Cos(30°) = " + Math.Cos(radians));
        Console.WriteLine("Rounded: " + Math.Round(Math.Cos(radians), 6));
    }
}

The output of the above code is −

Cos(30°) = 0.866025403784439
Rounded: 0.866025

Using Math.Cos() with Multiple Angles

Example

using System;

public class Demo {
    public static void Main() {
        double[] angles = {0.0, 30.0, 60.0, 90.0, 180.0};
        
        Console.WriteLine("Angle\tCosine Value");
        Console.WriteLine("-----\t------------");
        
        foreach(double angle in angles) {
            double radians = (angle * Math.PI) / 180;
            double cosValue = Math.Cos(radians);
            Console.WriteLine($"{angle}°\t{Math.Round(cosValue, 6)}");
        }
    }
}

The output of the above code is −

Angle	Cosine Value
-----	------------
0°	1
30°	0.866025
60°	0.5
90°	0
180°	-1

Using Math.Cos() with Radians Directly

Example

using System;

public class Demo {
    public static void Main() {
        Console.WriteLine("Using radians directly:");
        Console.WriteLine("Cos(0) = " + Math.Cos(0));
        Console.WriteLine("Cos(?/4) = " + Math.Round(Math.Cos(Math.PI / 4), 6));
        Console.WriteLine("Cos(?/2) = " + Math.Round(Math.Cos(Math.PI / 2), 15));
        Console.WriteLine("Cos(?) = " + Math.Cos(Math.PI));
    }
}

The output of the above code is −

Using radians directly:
Cos(0) = 1
Cos(?/4) = 0.707107
Cos(?/2) = 0
Cos(?) = -1

Conclusion

The Math.Cos() method in C# calculates the cosine of an angle provided in radians. Remember to convert degrees to radians when working with degree measurements. The method returns values between -1 and 1, making it essential for trigonometric calculations in mathematics and graphics programming.

Updated on: 2026-03-17T07:04:35+05:30

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements