Q:

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

belongs to collection: C# Basic Programs | basics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to calculate the traveled distance base... >>
<< C# program to calculate the Cosine(X) using a pred...