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

Here, we are going to learn how to calculate the Cosine(X) without using a predefined method in C#? By Nidhi Last updated : April 15, 2023

Here we calculate the Cosine(X) using the below formula,
Cos(x) = 1.0- p /2+ q /24- p * q /720+ q * q /40320- p * q * q /3628800;

## C# program to calculate the Cosine(X) without using a predefined method

The source code to calculate the Cosine(X) without using a predefined method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

```//C# program to calculate the Cosine(X)
//without using a predefined method.

using System;

class Demo
{
static double CalculateCosX(double X)
{
double P = 0.0F;
double Q = 0.0F;
double Result = 0.0F;

P = Math.Pow(X, 2);
Q = Math.Pow(P, 2);

Result = 1.0 - P / 2 + Q / 24 - P * Q / 720 + Q * Q / 40320 - P * Q * Q / 3628800;

return Result;
}
static void Main(string[] args)
{
double val = 0.0F;

while (val <= 5)
{
Console.WriteLine("Cosine({0}) => {1}", val, CalculateCosX(val));
val++;
}
}
}
```

## Output

```Cosine(0) => 1
Cosine(1) => 0.540302303791887
Cosine(2) => -0.41615520282187
Cosine(3) => -0.991049107142857
Cosine(4) => -0.6857848324515
Cosine(5) => -0.162746638007054
Press any key to continue . . .
```

## Explanation

here, we created a class Demo that contains two static methods CalculateCosX() and Main() method.

In the CalculateCosX() method, we calculate cosine using the below formula.
Cos(x) = 1.0- p /2+ q /24- p * q /720+ q * q /40320- p * q * q /3628800;

The Main() is the entry point of the program, here we called the CalculateCosX() method for values 0 to 5 using the "while" loop and print the results on the console screen.