C Program to Calculate the Value of cos(x)

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

Problem Description

This C Program calculates the value of cos(x).

Problem Solution

Take input from the user and calculates cos(x) value as shown in the program below.

Program/Source Code

Here is source code of the C program to calculate the value of cos(x). The C program is successfully compiled and run on a Linux system. The program output is also shown below.

 * C program to find the value of cos(x) using the series
 * up to the given accuracy (without using user defined function)
 * also print cos(x) using library function.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
    int n, x1;
    float accuracy, term, denominator, x, cosx, cosval;
    printf("Enter the value of x (in degrees) \n");
    scanf("%f", &x);
    x1 = x;
    /*  Converting degrees to radians */
    x = x * (3.142 / 180.0);
    cosval = cos(x);
    printf("Enter the accuracy for the result \n");
    scanf("%f", &accuracy);
    term = 1;
    cosx = term;
    n = 1;
        denominator = 2 * n * (2 * n - 1);
        term = -term * x * x / denominator;
        cosx = cosx + term;
        n = n + 1;
    } while (accuracy <= fabs(cosval - cosx));
    printf("Sum of the cosine series = %f\n", cosx);
    printf("Using Library function cos(%d) = %f\n", x1, cos(x));
Program Explanation

In this C program, we are reading the number of the terms in a series using ‘n’ variable. To convert degrees to radians the following formula is used


Cos(x) = x *(3.142/180.0).

Do while loop is used to compute the sum of cosine series. Compute the denominator by multiplying the difference of ‘n’ variable value by 1 with 2 and multiply again with ‘n’ variable value by 2.

Multiply the value of ‘x’ variable twice with the value of ‘term’ variable. Take negation of the value then divide the value by ‘denominator’ variable. Compute the summation of the value of ‘cosx’ variable with the value of ‘term’ variable.

While condition is used to check the value of ‘accuracy’ variable is less than or equal to fabs() function value. If the condition is true then the iteration of the loop. Print the value of cos(x) using printf statement.

Runtime Test Cases
$ cc pgm15.c -lm
$ a.out
Enter the value of x (in degrees)
Enter the accuracy for the result
Sum of the cosine series       = 0.451546
Using Library function cos(60) = 0.499882
$ a.out
Enter the value of x (in degrees)
Enter the accuracy for the result
Sum of the cosine series       = 0.691495
Using Library function cos(45) = 0.707035

Sanfoundry Global Education & Learning Series – 1000 C Programs.


Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.