C# Program to Find out the value of Sin(x)


Introduction

In this article, we will understand the C# program to find out the value of Sin(x). Sine is another name for Sin(x). It is a trigonometric angle formula. The sine of an angle is the proportion of the length of the hypotenuse to the length of the perpendicular in a right-angled trapezoid. The robust computer language C# can be used to resolve challenging mathematical issues. Finding the value of sin(x), where x is any angle in radians, is one of these issues. In this article, we'll look at how to use the Math library to create a C# program that calculates the value of sin(x). The mathematical foundations of the sin function will also be covered, along with some of the applications it can be used for in the actual world. Whether you are a novice or a seasoned programmer, this essay will give you useful tips on how to use C# for calculations. So let's get started and learn how to calculate sin(x) in C#

Methods

By using the built-in sin() function, we can determine the sine of an angle. This method is specified under the Math class and is a part of the system namespace. Because it covers constants and some static trigonometric, logarithmic, and other methods, math instruction is very helpful.

Apart from this method which will be used directly in our code, there is one more method that is important considering the output console, that is −

By Using Maclaurin expansion, we can determine the sine of an angle. Therefore, sin(x)'s Maclaurin series extension is

Algorithm

To calculate the value of sin(x), follow the instructions below −

Step 1  set the angle (in degrees) to be computed into the variable angleInDegree.

Step 2  Create a new variable called terms that stores how many terms we can use to estimate the value of sin.(x).

Step 3  Declare the findSinx global function.

Step 4  Establish a fluctuating stream. The orientation is saved there in radians.

Step 5  Use current to initialize a variable response. It will save our complete response.

Step 6  Use current to initialize another variable's temperature.

Step 7  Repeat from term 1 to term i. Update the temperature as ((-temp) * current * current) / ((2 * i) * (2 * i + 1)) at each stage, and update the answer as ((answer + temp)).

Step 8  Finally, give the result of the findSinX method.

Step 9  Print the solution.

Example

// C# program to illustrate how we can
// calculate the value of sin(x)
// using Maclaurin's method

using System;
class SINE{
   static double findSinX(int angleInDegree, int terms) {

      // Converting angle in degree into radian
      double current = Math.PI * angleInDegree / 180f;

      // Declaring variable to calculate final answer
      double answer = current;
      double temp = current;

      // Loop till number of steps provided by the user
      for(int i = 1; i <= terms; i++) {

         // Updating temp and answer accordingly
         temp = ((-temp) * current * current) / ((2 * i) * (2 * i + 1)); 
         answer = answer + temp;
      }
      
      // Return the final answer
      return answer;
   }
   
   // Driver code
   static public void Main() {

      // Angle in degree
      int angleInDegree1 = 45;
      
      // Number of steps
      int terms1 = 10;
      
      // Calling function to calculate sine of angle
      double answer1 = findSinX(angleInDegree1, terms1);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}", angleInDegree1, answer1);

      // Angle in degree
      int angleInDegree2 = 90;

      // Number of steps
      int terms2 = 20;
      
      // here we are calling function to calculate sine of angle
      double result2 = findSinX(angleInDegree2, terms2);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",
      angleInDegree2, result2);

      // Angle in degree
      int angleInDegree3 = 135;
      
      // Number of steps
      int terms3 = 30; 
      
      // Calling function to calculate sine of angle
      double result3 = findSinX(angleInDegree3, terms3);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",  angleInDegree3, result3);

      // Angle in degree
      int angleInDegree4 = 180;
      
      // Number of steps
      int terms4 = 40;

      // Calling function to calculate sine of angle
      double result4 = findSinX(angleInDegree4, terms4);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",  angleInDegree4, result4);
   }
}

Output

The value of sin(45) = 0.707106781186547
The value of sin(90) = 1
The value of sin(135) = 0.707106781186548
The value of sin(180) = 2.34898825287367E-16

Time Complexity

In this particular program of finding the value of Sin(x) we got the Time complexity: O(n). //n is the number of terms passed as input.

And the Space complexity is O(1).

Conclusion

In conclusion, creating a C# program to calculate sin(x) is a rather straightforward process that can be carried out with the Math library. Programmers can use this knowledge to construct more complex mathematical algorithms and applications by comprehending the mathematical ideas underlying the sin function.

Engineering, physics, and computer graphics are just a few of the real-world uses for knowing how to calculate sin values. Sin functions, for instance, are frequently employed to model wave motion, provide visual effects, and manage robotic systems.

In conclusion, learning how to use sin functions and the C# programming language can give programmers a valuable set of abilities that can be used for a variety of complicated mathematical problems across a range of sectors.

Updated on: 21-Apr-2023

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements