---
title: "Cosine series up to n terms"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:cosine-series"
source: https://www.bctengineer.com/q/semester_1/computer_programming/cosine-series
site: BCT Engineering
---

# Cosine series up to n terms

Computer Programming · Chapter 5: Control statements

- Asked in: 2073 Chaitra, 6 marks
- Video: [C Practical and Assignment Programs-cos(x)](https://www.youtube.com/watch?v=LtrJzQRu74k) — We The Computer Guys
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/cosine-series
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program in C-programming language to compute the cosine series. (hints: cos(x) = 1-x²/2!+x⁴/4!-x⁶/6!+…………up to n terms)

## Short answer

Compute cos(x) using series: start term=1, sum=1. Each new term = previous * (-x^2)/((2i-1)(2i)). For x=1.0 rad and 8 terms, the sum equals 0.540302.

## Answer

cos(x) = 1 - x²/2! + x⁴/4! - x⁶/6! + ... (x in radians). Instead of calculating factorials and powers separately, each term is obtained from the previous one:

term(i) = term(i-1) × (-x²) / ((2i-1)(2i)), with term(0) = 1

so the program needs only one loop.

```c
#include <stdio.h>
#include <math.h>
int main(void)
{
    int n, i;
    double x, term = 1.0, sum = 1.0;
    printf("Enter x (in radians) and number of terms: ");
    scanf("%lf %d", &x, &n);
    for (i = 1; i < n; i++)
    {
        /* next term = previous term * (-x*x) / ((2i-1)(2i)) */
        term = term * (-x * x) / ((2 * i - 1) * (2 * i));
        sum += term;
    }
    printf("cos(%.2f) by series = %.6f\n", x, sum);
    printf("cos(%.2f) by library = %.6f\n", x, cos(x));
    return 0;
}
```
Output for x = 1.0 and 8 terms:
```text
Enter x (in radians) and number of terms: cos(1.00) by series = 0.540302
cos(1.00) by library = 0.540302
```

The series value matches the library `cos()` value.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is each term obtained?
   A. term = term * x / i
   B. term = term * (-x*x) / ((2i-1)(2i))
   C. term = term + x*x
   D. term = x^i / i
   Answer: B. Each term uses previous, avoiding factorials. (Key point: Term recurrence)

2. What are the starting values of term and sum?
   A. Both 0
   B. term 0, sum 1
   C. Both 1.0
   D. term x, sum x
   Answer: C. First term is 1. (Key point: Initial)

3. In what unit must x be?
   A. Degrees
   B. Grads
   C. Any
   D. Radians
   Answer: D. The series needs radians. (Key point: Angle unit)

4. Why use the recurrence?
   A. One loop, no separate factorial/power
   B. Needs math.h
   C. Avoids sum
   D. Faster scanf
   Answer: A. Reduces computation and overflow. (Key point: Why recurrence)

## Related in Computer Programming

- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, asked 3×
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Computer program and programming](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-program-programming) — Computer Programming, chapter 2
- [FORTRAN characteristics; compute π](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-characteristics-pi) — Computer Programming, chapter 11
- [Series 1+11+111+… by recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aseries-1-11-111-recursive) — Computer Programming, chapter 6
- [Series (2*3/5)+(4*5/7)+… with functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aseries-2x3-over-5-recursive) — Computer Programming, chapter 6

## Related in other subjects

- [Half range cosine series: πx − x²](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Ahalf-range-cosine-series-x-x2) — Mathematics III, chapter 4, asked 3×
- [Half range cosine series](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Ahalf-range-cosine-series) — Mathematics III, chapter 4, asked 3×
- [Half range cosine series: x sin x](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Ahalf-range-cosine-series-x-sin) — Mathematics III, chapter 4, asked 3×
- [Half range cosine series: eˣ](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Ahalf-range-cosine-series-e) — Mathematics III, chapter 4, asked 3×
