HomeC ProgrammingC Program to Calculate the Sum of cos(x) Series

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

This C program calculates the sum of the cos(x) series for a given angle and number of terms. It provides an iterative approach to compute the sum using a for loop and mathematical operations.

The cosine function is a mathematical function that relates the angles of a right triangle to the ratios of its sides. It is a periodic function that oscillates between the values of -1 and 1. The cosine series is an infinite series that represents the cosine function as a sum of terms.

Program Statement

Program Statement: Cosine Series Sum Calculation

Write a C program that calculates the sum of the cosine series up to a specified number of terms. The program should prompt the user to enter the value of x and the number of terms they want to sum. It should then calculate and display the sum of the cosine series up to the specified number of terms.

The cosine series is given by the formula:

cos(x) = 1 – x^2/2! + x^4/4! – x^6/6! + …

where x is the angle in radians.

The program should contain the following:

  1. A function cosineSeriesSum that takes two parameters: the angle x (a double) and the number of terms (an integer). This function calculates and returns the sum of the cosine series up to the specified number of terms using the provided formula.
  2. In the main function, prompt the user to enter the value of x and the number of terms they want to sum.
  3. Read the input values from the user.
  4. Call the cosineSeriesSum function with the provided input values and store the result.
  5. Display the sum of the cosine series up to the specified number of terms on the console with a precision of 6 decimal places.

Ensure proper formatting and meaningful prompts for user input and output.

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

#include <stdio.h>
#include <math.h>

double calculateCosSeries(double x, int numTerms);

int main() {
    double x;
    int numTerms;

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    printf("Enter the number of terms: ");
    scanf("%d", &numTerms);

    double sum = calculateCosSeries(x, numTerms);

    printf("Sum of the cos(x) series: %.4f\n", sum);

    return 0;
}

double calculateCosSeries(double x, int numTerms) {
    double sum = 0.0;

    for (int n = 0; n < numTerms; n++) {
        double term = pow(-1, n) * pow(x, 2 * n) / tgamma(2 * n + 1);
        sum += term;
    }

    return sum;
}

How it works ?

  1. The program starts by defining the cosineSeriesSum function, which takes in two parameters: x (the angle) and terms (the number of terms to sum).
  2. Inside the cosineSeriesSum function, a variable sum is initialized to 0.0 to hold the running sum of the cosine series.
  3. A loop is set up to iterate i from 0 to terms - 1, representing the indices of the terms in the series.
  4. Within the loop, the power of x is calculated as 2 * i, since the powers alternate between even numbers in the cosine series.
  5. The term of the series is computed as pow(x, power) / tgamma(power + 1), where pow calculates the power of x and tgamma calculates the factorial of power + 1. The factorial is divided to ensure the correct value of each term.
  6. If i is odd (i.e., i % 2 == 1), the sign of the term is flipped by multiplying it by -1.
  7. The term is added to the sum.
  8. After the loop completes, the final value of sum is returned as the result of the function.
  9. In the main function, the user is prompted to enter the value of x and the number of terms.
  10. The input values are read from the user using the scanf function.
  11. The cosineSeriesSum function is called with the input values, and the result is stored in the variable result.
  12. The calculated sum of the cosine series is displayed on the console using printf, with a precision of 6 decimal places.

That’s it! The program calculates the sum of the cosine series up to the specified number of terms using the provided formula and displays the result to the user.

Input/Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...