C Exercises: Calculate the sum of the series [ x - x^3 + x^5 + ......]

C For Loop: Exercise-24 with Solution

Write a program in C to find the sum of the series [ x - x^3 + x^5 + ......].

Sample Solution:

C Code:

#include <stdio.h>
#include <math.h>
void main()
	int x,sum,ctr;
	int i,n,m,mm,nn;
	printf("Input the value of x :");
	printf("Input number of terms : ");
	sum =x; m=-1;
	printf("The values of the series: \n");
    for (i = 1; i < n; i++) 
        ctr = (2 * i + 1);
        mm = pow(x, ctr);
        nn = mm * m;
        printf("%d \n",nn);
        sum = sum + nn;
        m = m * (-1);
	printf("\nThe sum = %d\n",sum);

Sample Output:

Input the value of x :2                                                                                       
Input number of terms : 5                                                                                     
The values of the series:                                                                                     
The sum = 410                                                                                           


Flowchart: Calculate the sum of the series [ x - x^3 + x^5 + ......]

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....].
Next: Write a program in C to display the n terms of square natural number and their sum.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

Clearing a char array c

It depends on how you want to view the array. If you are viewing the array as a series of chars, then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this.

On the other hand, if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.

Ref : https://bit.ly/3uM7JnL