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

Advertisements
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.
Advertisements
Subscribe Newsletter & Posts

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.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry