w3resource

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

This C program calculates the sum of the series x - x^3 + x^5 - ... up to a specified number of terms. The user inputs the value of x and the number of terms n. The program then uses a loop to compute each term of the series, alternating the sign and increasing the exponent by 2 for each subsequent term. Finally, the program sums the terms and prints the result.

Sample Solution:

C Code:

#include <stdio.h> // Include the standard input/output header file.
#include <math.h>  // Include the math functions header file.

void main()
{
    int x, sum, ctr; // Declare variables to store input and intermediate results.

    int i, n, m, mm, nn; // Declare loop control variables and intermediate results.

    printf("Input the value of x :"); // Prompt the user for input.
    scanf("%d", &x); // Read the value of 'x' from the user.

    printf("Input number of terms : "); // Prompt the user for input.
    scanf("%d", &n); // Read the value of 'n' from the user.

    sum = x; // Initialize 'sum' to 'x' as the first term in the series.
    m = -1;  // Initialize 'm' to -1.

    printf("The values of the series: \n"); // Print a message.

    printf("%d\n", x); // Print the first term of the series.

    for (i = 1; i < n; i++) // Loop to calculate subsequent terms in the series.
    {
        ctr = (2 * i + 1); // Calculate the power of 'x' using the given formula.
        mm = pow(x, ctr); // Calculate the value of 'x' raised to the power 'ctr'.
        nn = mm * m;      // Multiply 'mm' with 'm'.
        printf("%d \n", nn); // Print the calculated term.
        sum = sum + nn;      // Add the term to the running sum.
        m = m * (-1);        // Toggle the value of 'm' between -1 and 1.
    }

    printf("\nThe sum = %d\n", sum); // Print the final sum.
}

Output:

Input the value of x :2                                                                                       
Input number of terms : 5                                                                                     
The values of the series:                                                                                     
2                                                                                                             
-8                                                                                                            
32                                                                                                            
-128                                                                                                          
512                                                                                                           
                                                                                                              
The sum = 410                                                                                           

Flowchart:

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

C Programming Code Editor:

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.



Follow us on Facebook and Twitter for latest update.