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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.