Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert Trigonometric Angles in Radians using C#?
To convert trigonometric angles from degrees to radians in C#, multiply the degree value by Math.PI/180. Most trigonometric functions in C# expect angles in radians, so this conversion is essential for accurate calculations.
Syntax
Following is the syntax for converting degrees to radians −
double radians = degrees * (Math.PI / 180.0);
Following is the syntax for converting radians to degrees −
double degrees = radians * (180.0 / Math.PI);
Converting Degrees to Radians
Example
The following example shows how to properly convert degrees to radians before using trigonometric functions −
using System;
class Program {
static void Main() {
double angleInDegrees = 45;
// Incorrect: using degrees directly
Console.WriteLine("Cos(45 degrees directly): " + Math.Cos(45));
// Correct: convert degrees to radians first
double angleInRadians = angleInDegrees * (Math.PI / 180.0);
Console.WriteLine("Cos(45 degrees converted): " + Math.Cos(angleInRadians));
// Alternative using Math.PI constant
double result = Math.Cos(Math.PI * 45 / 180.0);
Console.WriteLine("Using Math.PI formula: " + result);
}
}
The output of the above code is −
Cos(45 degrees directly): 0.525321988818042 Cos(45 degrees converted): 0.7071067811865476 Using Math.PI formula: 0.7071067811865476
Converting Radians to Degrees
Example
You can also convert radians back to degrees for display purposes −
using System;
class Program {
static void Main() {
double radiansValue = Math.PI / 4; // 45 degrees in radians
// Convert radians to degrees
double degreesValue = radiansValue * (180.0 / Math.PI);
Console.WriteLine("Radians: " + radiansValue);
Console.WriteLine("Degrees: " + degreesValue);
// Verify with trigonometric function
Console.WriteLine("Sin(" + degreesValue + "°) = " + Math.Sin(radiansValue));
}
}
The output of the above code is −
Radians: 0.7853981633974483 Degrees: 45 Sin(45°) = 0.7071067811865475
Common Angle Conversions
Example
Here are some commonly used angle conversions −
using System;
class Program {
static void Main() {
double[] commonAngles = {0, 30, 45, 60, 90, 180, 270, 360};
Console.WriteLine("Degrees\tRadians\t\tSin\t\tCos");
Console.WriteLine("-------\t-------\t\t---\t\t---");
foreach(double angle in commonAngles) {
double radians = angle * (Math.PI / 180.0);
Console.WriteLine($"{angle}°\t{radians:F4}\t\t{Math.Sin(radians):F4}\t\t{Math.Cos(radians):F4}");
}
}
}
The output of the above code is −
Degrees Radians Sin Cos ------- ------- --- --- 0° 0.0000 0.0000 1.0000 30° 0.5236 0.5000 0.8660 45° 0.7854 0.7071 0.7071 60° 1.0472 0.8660 0.5000 90° 1.5708 1.0000 0.0000 180° 3.1416 0.0000 -1.0000 270° 4.7124 -1.0000 -0.0000 360° 6.2832 -0.0000 1.0000
Conclusion
Converting degrees to radians in C# is essential for accurate trigonometric calculations. Use the formula radians = degrees * (Math.PI / 180.0) to convert degrees to radians, and degrees = radians * (180.0 / Math.PI) to convert back to degrees when needed for display or user interface purposes.
