C Program to Calculate the Sum of cos(x) Series

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

Problem Description

This C Program calculates the sum of cos(x) series.

Problem Solution

Take input from the user and perform operations as shown in the program below.

Program/Source Code

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

/*
 * C program to find the sum of cos(x) series
 */
#include <stdio.h>
#include <math.h>
 
void main()
{
    int n, x1, i, j;
    float x, sign, cosx, fact;
 
    printf("Enter the number of the terms in a series\n");
    scanf("%d", &n);
    printf("Enter the value of x(in degrees)\n");
    scanf("%f", &x);
    x1 = x;
	/*  Degrees to radians */
    x = x * (3.142 / 180.0);
    cosx = 1;
    sign = -1;
    for (i = 2; i <= n; i = i + 2)
    {
        fact = 1;
        for (j = 1; j <= i; j++)
        {
            fact = fact * j;
        }
        cosx = cosx + (pow(x, i) / fact) * sign;
        sign = sign * (-1);
    }
    printf("Sum of the cosine series = %7.2f\n", cosx);
    printf("The value of cos(%d) using library function = %f\n", x1,
    cos(x));
}
Program Explanation

In this C program, library function defined in <math.h> header file is used to compute mathematical functions. We are reading the number of the terms and the degree value of the series using ‘n’ and ‘x’ variables. To find the sum of cos(x) series, the following formula is used.
Cos(x) = cosx + (pow (x, i) / fact) * sign

advertisement
advertisement
Runtime Test Cases
 
$ cc pgm63.c -lm
$ a.out
Enter the number of the terms in a series
3
Enter the value of x(in degrees)
90
Sum of the cosine series =   -0.23
The value of cos(90) using library function = -0.000204

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
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.

If you find any mistake above, kindly email to [email protected]

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
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.