# C# program to 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#?
Submitted by Nidhi, on September 22, 2020

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;

Program:

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.

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