C# Program to Find out the value of Sin(x)

In this article, we will learn how to create a C# program to find the value of Sin(x). The sine function is a fundamental trigonometric function that calculates the ratio of the opposite side to the hypotenuse in a right triangle. C# provides built-in methods to calculate sine values, and we can also implement our own calculation using mathematical series.

Syntax

C# provides a built-in method in the Math class to calculate sine values

Math.Sin(double angleInRadians)

The Maclaurin series expansion for sin(x) is

sin(x) = x - x³/3! + x?/5! - x?/7! + x?/9! - ...

Using Math.Sin() Method

The simplest way to calculate sine values is using the built-in Math.Sin() method. Note that this method expects the angle in radians, not degrees

Angle Conversion Degrees User Input Radians Math.Sin() ?/180 radians = degrees × ?/180

Example

using System;

class SineCalculation {
    public static void Main() {
        // Angles in degrees
        double[] angles = {0, 30, 45, 60, 90, 180};
        
        Console.WriteLine("Angle(°)\tSin(x)");
        Console.WriteLine("================");
        
        foreach(double angle in angles) {
            // Convert degrees to radians
            double radians = angle * Math.PI / 180.0;
            
            // Calculate sine using built-in method
            double sineValue = Math.Sin(radians);
            
            Console.WriteLine("{0}°\t\t{1:F6}", angle, sineValue);
        }
    }
}

The output of the above code is

Angle(°)        Sin(x)
================
0°              0.000000
30°             0.500000
45°             0.707107
60°             0.866025
90°             1.000000
180°            0.000000

Using Maclaurin Series Implementation

We can implement our own sine calculation using the Maclaurin series expansion. This approach helps understand the mathematical foundation behind the sine function

Example

using System;

class SineMaclaurin {
    static double CalculateSin(double angleInDegrees, int terms) {
        // Convert degrees to radians
        double x = Math.PI * angleInDegrees / 180.0;
        
        double result = x;  // First term of series
        double term = x;    // Current term
        
        // Calculate remaining terms using Maclaurin series
        for(int i = 1; i <= terms; i++) {
            term = (-term * x * x) / ((2 * i) * (2 * i + 1));
            result += term;
        }
        
        return result;
    }
    
    public static void Main() {
        double[] testAngles = {45, 90, 135, 180};
        int numTerms = 15;
        
        Console.WriteLine("Angle\tMaclaurin\tMath.Sin()");
        Console.WriteLine("=====================================");
        
        foreach(double angle in testAngles) {
            double maclaurinResult = CalculateSin(angle, numTerms);
            double builtInResult = Math.Sin(angle * Math.PI / 180.0);
            
            Console.WriteLine("{0}°\t{1:F6}\t{2:F6}", 
                angle, maclaurinResult, builtInResult);
        }
    }
}

The output of the above code is

Angle   Maclaurin       Math.Sin()
=====================================
45°     0.707107        0.707107
90°     1.000000        1.000000
135°    0.707107        0.707107
180°    0.000000        0.000000

Comparison of Methods

Method Accuracy Performance Use Case
Math.Sin() Very High Fast Production applications
Maclaurin Series Depends on terms Slower Educational/Custom implementations

Common Use Cases

  • Computer Graphics: Rotating objects and creating smooth animations

  • Signal Processing: Generating and analyzing wave patterns

  • Physics Simulations: Modeling oscillatory motion and wave mechanics

  • Game Development: Character movement and projectile trajectories

Conclusion

C# provides both built-in methods and the flexibility to implement custom sine calculations. The Math.Sin() method is recommended for production use due to its accuracy and performance, while implementing the Maclaurin series helps understand the mathematical principles behind trigonometric calculations.

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

634 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements