CSharp - Write program to Calculate Sine function with Taylor expansion

Requirements

You will write a program that calculates sine function with Taylor expansion.

Hint

To perform the task, you can use a Taylor expansion.

The value of the sine function at a given point x (x is an angle in radians) can be calculated as a sum of infinite series.

                                                                                          
                 x*x*x   x*x*x*x*x     x*x*x*x*x*x*x 
sin x = x -    -------- + --------   +---------------+ ...
                  3!       5!            7!            
 

7! = 1 * 2 * 3 * ... * 7.

Demo

using System;
class Program//from  w  w w  .  j av  a 2s.c  o  m
{
    static void Main(string[] args)
    {
        // Input 
        Console.Write("Enter an angle in degrees: ");
        string input = Console.ReadLine();
        double angle = Convert.ToInt32(input);

        // Converting to radians 
        double x = Math.PI / 180 * angle;

        // Preparations 
        double member;
        double sum = 0;
        double tinyValue = 1e-15;

        double sign = 1;
        double power = x;
        double factorial = 1;
        double multiplier = 1;

        // Sum of the series 
        do
        {
            // Calculating current member of the series 
            member = sign * power / factorial;

            // Appending to sum 
            sum += member;

            // Preparing next step 
            sign *= -1;
            multiplier++;
            factorial *= multiplier;
            multiplier++;
            factorial *= multiplier;

            power *= x * x;

        } while (Math.Abs(member) > tinyValue);

        // Output 
        Console.WriteLine("Our value: " + sum.ToString());
        Console.WriteLine("Math.Sin:  " + Math.Sin(x).ToString());

    }
}

Result