C# program to find the value of sin(x)

Here, we are going to learn how to find the value of sin(x) in C#?
Submitted by Nidhi, on September 21, 2020

Here we will find the value of SIN(X) using the below series.
sin(x)=x-x^3/3!+x^5/5-x^7/7!

Program:

The source code to find the value of SIN(x), is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to calculate the series of sin(x) in C#

using System;

class Sine
{
    static double CalculateSinX(int deg, int terms)
    {
        double x;
        double result;
        double temp;

        int loop;
        x = Math.PI * deg / 180f;
        result = x;
        temp   = x;

        for (loop = 1; loop <= terms; loop++)
        {
            temp = (-temp * x * x) / ((2 * loop) * (2 * loop + 1));
            result = result + temp;
        }

        return result;
    }
    public static void Main()
    {
        int degree  =   0;
        int terms   =   0;

        double result = 0.0;

        Console.Write("Enter the angle in Degrees:");
        degree = int.Parse(Console.ReadLine());

        Console.Write("Enter the number of terms:");
        terms = int.Parse(Console.ReadLine());

        result = CalculateSinX(degree, terms);
        Console.WriteLine("Sin({0})={1}", degree, result);
    }
}

Output:

Enter the angle in Degrees:90
Enter the number of terms:20
Sin(90)=1
Press any key to continue . . .

Explanation:

Here, we created a class Sin that contains two static methods CalculateSinX() and Main().

The CalculateSinX() method is used to calculate the value of SIN() based on a specified degree and number terms using the below series.

sin(x)=x-x^3/3!+x^5/5-x^7/7!

In the Main() method, we read the value of the degree and terms from the user and the calculate value of SIN(X) using CalculateSinX() and print the result on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.