Open In App

C# Program to Calculate the Cosine(X) Without Using a Predefined Method

Improve
Improve
Like Article
Like
Save
Share
Report

Cosine(x) is also known as Cos(x). It is a trigonometric function of an angle. The ratio of the length of the base to the length of the hypotenuse is known as the cosine of an angle in the right-angle triangle. In this article, we will learn to calculate the Cosine(X) without using a predefined method. So to do this task we use the following formula:

cos(x) = 1.0 – p /2 + q /24 – p * q /720 + q * q /40320 – p * q * q /3628800

Example:

Input  : cos(45)
Output : 0.7071

Input  : cos(0)
Output : 1

Code:

C#




// C# program to calculate the Cosine(X)
// Without using a predefined method
using System;
 
class GFG{
 
// Function to calculate the Cosine(X)    
static double Cos(double theta)
{
    double R;
    double S;
    double ans;
 
    R = Math.Pow(theta, 2);
    S = Math.Pow(R, 2);
 
    // Substituting p,q in the below formula
    ans = 1.0 - R / 2 + S / 24 - R * S / 720 +
            S * S / 40320 - R * S * S / 3628800;
 
    return ans;
}
 
// Driver code
static void Main(string[] args)
{
    Console.WriteLine("Cos(0):" + Cos(0));
    Console.WriteLine("Cos(3):" + Cos(1));
    Console.WriteLine("Cos(8):" + Cos(2));
}
}


Output:

Cos(0):1
Cos(3):0.540302303791887
Cos(8):-0.41615520282187

Last Updated : 23 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads