# C++ program for Cosine Series

Before going to the program for *Cosine Series* first let us understand what is a *Cosine Series**?*

**Cosine Series:**

*Cosine Series* is a series which is used to find the value of Cos(x).

where, **x** is the angle in **degree** which is converted to **Radian**.

The formula used to express the Cos(x) as Cosine Series is

Expanding the above notation, the formula of Cosine Series is

For example,

Let the value of **x **be** 30**.

So, Radian value for **30** degree is **0.52359.**

So, the value of **Cos(30)** is **0.8660.**

## Program code for Cosine Series in C:

#include<iostream.h> #include<iomanip.h> #include<conio.h> void main() { int i, n; float x, sum=1, t=1; clrscr(); cout<<" Enter the value for x : "; cin>>x; cout<<" Enter the value for n : "; cin>>n; x=x*3.14159/180; /* Loop to calculate the value of Cosine */ for(i=1;i<=n;i++) { t=t*(-1)*x*x/(2*i*(2*i-1)); sum=sum+t; } cout<<" The value of Cos("<<x<<") = "<<setprecision(4)<<sum; getch(); }

**Note: setprecision(4)** is used to **set the floating point number upto 4 decimal points.**

**iomanip.h** is a header file which contains the **setprecision() function.**

**Related: C++ program for Sine Series**

## Working:

- First the computer reads the value of ‘x’ and ‘n’ from the user.
- Then ‘x’ is converted to radian value.
- Then using for loop the value of Cos(x) is calculate.
- Finally the value of Cos(x) is printed.

**Related: C++ program for Exponential Series**

## Step by Step working of the above Program Code:

Let us assume that the user enters the value of ‘x’ as **45** and ‘n’ as **3.**

- Converting ‘x’ to radian value

x = x * 3.14159 / 180 (x = 45 * 3.14159 / 180) So, **x=0.78539**

- It assigns
**t=1**and**sum=1** - It assigns the value of
**i=1**and the loop continues till the condition of the for loop is true.

3.1. i<=n (**1<=3**) for loop condition is true

t = (1 * (-1)^{ }* 0.78539 * 0.78539)/(2 * 1 * (2 * 1 – 1))

So, **t = – 0.30841**

sum = 1 + (- 0.30841)

So, **sum=0.69159**

i++

So, **i=2**

3.2. i<=n (**2<=3**) for loop condition is true

t = (- 0.30841 * (-1)^{ }* 0.78539 * 0.78539)/(2 * 2 * (2 * 2 – 1))

So, **t = 0.01585**

sum = 0.69159 + 0.01585

So, **sum=0.70744**

i++

So, **i=3**

3.3. i<=n (**3<=3**) for loop condition is true

t = (0.01585 * (-1)^{ }* 0.78539 * 0.78539)/(2 * 3 * (2 * 3 – 1))

So, **t = – 0.000325**

sum = 0.70744 + (- 0.000325)

So, **sum=0.70711**

i++

So, **i=4**

3.4. i<=n (**4<=3**) for loop condition is false

It comes out of the for loop.

- Finally it prints
**The value of Cos(0.78539) = 0.7071**

- Thus program execution is completed.

## Output:

### TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

Excellent work

I have found the solution what i am looking for

its really a good work

Really helpful