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

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

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.

  1. /*
  2.  * C program to find the sum of cos(x) series
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. void main()
  8. {
  9.     int n, x1, i, j;
  10.     float x, sign, cosx, fact;
  11.  
  12.     printf("Enter the number of the terms in a series\n");
  13.     scanf("%d", &n);
  14.     printf("Enter the value of x(in degrees)\n");
  15.     scanf("%f", &x);
  16.     x1 = x;
  17. 	/*  Degrees to radians */
  18.     x = x * (3.142 / 180.0);
  19.     cosx = 1;
  20.     sign = -1;
  21.     for (i = 2; i <= n; i = i + 2)
  22.     {
  23.         fact = 1;
  24.         for (j = 1; j <= i; j++)
  25.         {
  26.             fact = fact * j;
  27.         }
  28.         cosx = cosx + (pow(x, i) / fact) * sign;
  29.         sign = sign * (-1);
  30.     }
  31.     printf("Sum of the cosine series = %7.2f\n", cosx);
  32.     printf("The value of cos(%d) using library function = %f\n", x1,
  33.     cos(x));
  34. }


$ 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 Reference 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 Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Follow Manish & Sanfoundry