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++;
}
}
}
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.
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.
Output:
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.