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#? By Nidhi Last updated : April 15, 2023

Value of sin(x)

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!

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

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.

C# Basic Programs »


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.