C# Program to Calculate the Value of cos(x)

This is a C# Program to find the value of cos(x).

Problem Description

This C# Program Finds the Value of Cos(x).

Problem Solution

Here the cos value is found without using the library math function.

Program/Source Code

Here is source code of the C# Program to Find the Value of Cos(x) . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find the Value of Cos(x)
 */
using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (double d = 0; d < 6.0; d += 0.5)
            {
                Console.WriteLine("The cosine of {0} = {1}", d, Math.Cos(d));
                Console.WriteLine("Calculated cosine of {0} = {1}", d, cos(d));
                Console.WriteLine();
            }
            Console.ReadKey(); 
        }
 
        static double cos(double x) 
        {
            double p = x * x; 
            double q = p * p;
            return 1.0 - p / 2 + q / 24 - p * q / 720 + q * q / 40320 
            - p * q * q / 3628800;
        }
    }
}
Program Explanation

In this C# program, library function defined in <math.h> header file is used. The following formula is used to compute the value of Cos(x)
Cos(x) = 1.0- p /2+ q /24- p * q /720+ q * q /40320- p * q * q /3628800

advertisement
Runtime Test Cases
 
The Cosine of 0 = 1
Calculated Cosine of 0 = 1
The Cosine of 0.5 = 0.877582561890373
Calculated Cosine of 0.5 = 0.877582561889864
The Cosine of 1.5 = 0.54030230586814
Calculated Cosine of 1.5 = 0.540302303791887
The Cosine of 2.0 = 0.0707372016677029
Calculated Cosine of 2.0 = 0.0707369341169085

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.