The program to compute cosine series is based on the mathematical concept of sequences and series – particularly power series.

Problem Definition

Cosine series is a power series known as the Maclaurin expansion of \Large cos \hspace{2px} (x) where \Large x is a real number. For any given value of \Large x, the series will converges and evaluates to a finite value; the radius of convergence for the series is infinite.

Cosine Series
Figure 1 – Cosine Series

We won’t be discussing, how terms of the series are generated because that would be topic for another post. The Maclaurin expansion for \Large cos \hspace{2px} (x) is for \Large x =0, however, it is true for all real values of \Large x.

In other words, it is same as finding the value of trigonometric ratio of \Large cos \hspace{2px} \theta, the cosine series will give you a better approximate value of \Large cos \hspace{2px} \theta.

Flowchart – Program for Cosine Series

Flowchart - C Program for Cosine Series
Flowchart – C Program for Cosine Series

Program Codes for Cosine Series

/* Program to compute cosine series */
/* cos(x) = 1-x^2/2!+x^4/4!+x^6/6!+...+x^n/n! */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
    float x, t, sum;
    int d;
    int i, n=20;
    /* Read the Input x value in degrees */
    printf ("Input X Value (in degrees) :");
    scanf ("%f", &x);
    d= x;
    /*converting x to radians */
    x=x*3.1412/180;
    t=1;
    sum=1;
    for (i=1; i<n+1; i++)
    {
        t=t*pow ((double) (-1), (double) (2*i-1))*x*x/ (2*i*(2*i-1));
        sum=sum+t;
    }
    /* Print the Results */
    for (i =0; i<35; i++)
    printf ("_"); printf ("\n\n");
    printf ("COS (%d) =%7.3f\n\n", (int) d, sum);
    for (i =0; i<35; i++)
    printf ("_"); printf ("\n\n");
    system ("PAUSE");
    return 0;
}
/* C++ Program to compute cosine series */
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main() {
    float x, t, sum;
    int d;
    int i, n = 20;

    cout << "Input X Value (in degrees): ";
    cin >> x;
    d = static_cast<int>(x);

    // Convert degrees to radians
    x = x * 3.1412 / 180;
    t = 1;
    sum = 1;

    for (i = 1; i <= n; i++) {
        t = t * pow(-1.0, 2*i-1) * x * x / (2*i*(2*i-1));
        sum = sum + t;
    }

    for (i = 0; i < 35; i++) cout << "_";
    cout << "\n\n";
    cout << "COS (" << d << ") = " << fixed << sum << "\n\n";
    for (i = 0; i < 35; i++) cout << "_";
    cout << "\n\n";

    system("PAUSE");
    return 0;
}
/* Java Program to compute cosine series */
import java.util.Scanner;

public class CosineSeries {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float x, t, sum;
        int d;
        int n = 20;

        System.out.print("Input X Value (in degrees): ");
        x = sc.nextFloat();
        d = (int) x;

        // Convert degrees to radians
        x = x * 3.1412f / 180;
        t = 1;
        sum = 1;

        for (int i = 1; i <= n; i++) {
            t = t * (float)Math.pow(-1.0, 2*i-1) * x * x / (2*i*(2*i-1));
            sum = sum + t;
        }

        for (int i = 0; i < 35; i++) System.out.print("_");
        System.out.println("\n\n");
        System.out.printf("COS (%d) = %.3f\n\n", d, sum);
        for (int i = 0; i < 35; i++) System.out.print("_");
        System.out.println();
    }
}
# Python Program to compute cosine series
import math

x = float(input("Input X Value (in degrees): "))
d = int(x)

# Convert degrees to radians
x = x * 3.1412 / 180
t = 1
sum_ = 1
n = 20

for i in range(1, n+1):
    t = t * (-1)**(2*i-1) * x*x / (2*i*(2*i-1))
    sum_ += t

print("_"*35 + "\n")
print(f"COS ({d}) = {sum_:.3f}\n")
print("_"*35 + "\n")

Output

           Input X Values(in degrees):34
           -------------------------------------
           COS(34)= 0.829
           -------------------------------------