C# Program to Calculate the Series sin(x)=x-x^3/3!+x^5/!-x^7/7!+??
Posted by Superadmin on August 14 2022 03:38:02

C# Program to Calculate the Series sin(x)=x-x^3/3!+x^5/!-x^7/7!+……

 

This is a C# Program to calculate the series sin(x)=x-x^3/3!+x^5/!-x^7/7!+…….

Problem Description

This C# Program Finds the Value of sin(x) from the series sin(x)=x-x^3/3!+x^5/5-x^7/7!

Problem Solution

Take input from the user and perform series calculations as shown in the program below.

Program/Source Code

Here is source code of the C# program to Find the Value of sin(x) from the Series. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to calculate the series sin(x)=x-x^3/3!+x^5/!-x^7/7!+......
 */
using System;
class sine
{
    int deg, n;
    public void readdata()
    {
        Console.WriteLine("Enter the Number of Terms:");
        n = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter the Angle in Degrees:");
        deg = Convert.ToInt32(Console.ReadLine());
    }
    public void sineseries()
    {
        float x, s = 0.0f, t;
        x = (float)Math.PI * deg / 180f;
        s = x;
        t = x;
        for (int i = 1; i <= n; i++)
        {
            t = (-t * x * x) / ((2 * i) * (2 * i + 1));
            s = s + t;
        }
        Console.WriteLine("Sin({0})={1}", deg, s);
    }
    public static void Main()
    {
        sine s = new sine();
        s.readdata();
        s.sineseries();
    }
}
Program Explanation

This C# program, we are reading the number of terms and the angle in degrees using ‘n’ and ‘deg’ variables respectively. The sineseries() procedure is used to compute the sin(x) series value.

 

Using math function, compute the degree by multiplying the value of PI with the value of ‘deg’ variable. Divide the value by 180 and assigning the value to ‘x’ variable. Using for loop compute the sin() series.

Multiply the value of ‘i’ variable with the value of 2. Again multiply the value of ‘i’ variable with the value of 1. Divide the resulted value and assign to ‘t’ variable. Compute the addition of the value of ‘s’ variable with the value of ‘t’ variable.

Runtime Test Cases
 
Enter the Number of Terms:
20
Enter the Angle in Degrees:
90
Sin(90)=0.99999994