# C++ Program to Compute the Sine and Cosine Series

The program for sine and cosine is based on power series especially Taylor series. A power series is a form of representation of some functions that converges into a single value.

In simple words, some functions are in the form of an infinite series (A power series is also a form of infinite series) can give a finite value.

This program computes that finite value for a sine and cosine series and prints the result. To understand the mathematical part of the program, you must learn – sequences and series, calculus, infinite series, power series (Taylor series, Maclaurin series).

If you are not familiar with above concepts continue reading the following sections – problem definition, flowchart, program source code and verifies the output.This program is intended for intermediate level learners of C programming language.

## Problem Definition

The formula for computing the sine and cosine series for a given degree, X is

First, you change the value of x to radian and then using the result compute the sine and cosine series. To understand the concept, let’s take an example.

Suppose x = 45 degree

We want to compute sine (45), then convert 45 degrees into radian measure.

radian = 45 * π/180

= π/4

= 3.14/4

= 0.785398

Now, it is easy to compute the value of sin (π/4). Use the radian value in the series given above figure and get the following results.

sin (x) = 0.785398 – (0.785398)3/3! + (0.785398)5/5! …

sin (x) = 0.7072 (computed using calculator)

Note: The same principle applies to cosine series.

#### How do we process the input values ?

The process of computing sine and cosine series is described in 4 steps.

1. Convert the degree to radian value for sine and cosine series computation.
2. Compute the value of sin (x), where x is a value in radians.
3. Compute the value of cos (x), where x is a value in radians.
4. Print the result of the computation.

## Program Code – Sine and Cosine Series

/* Program to Compute Sine AND Cosine Series using following formula

Sin x = x - x^3/3! + x^5/5! -x^7/7! +...

Cosine x = x - x^2/2! + x^4/4! - x^6/6! + ... */

#include
#include
#include
#include
#include

#define PI 3.14

// Function Declaration Section

float factorial(int exponent);
int menu();
void compute_sine();
void compute_cosine();

//Global Declaration Section

int deg; float rad,deno,numa,result;
int exponent;
int n,i,t;

// MAIN FUNCTION BEGINS HERE

int main()

{

menu();

system("PAUSE");

return 0;

}

//Main ends here

void compute_sine()

{

n = 11;

result = 0.0;

//Compute the radian equivalent for the "deg" entered

rad = deg * PI/180;

//Compute the series for Radian measure of Sine

for( i = 0;i&lt; n;i++) {

exponent = (2 * i) + 1;

numa = pow(rad,exponent);

deno = factorial(exponent);

result = result + ((numa * pow(-1,i))/deno);

}

//Display the results

cout &lt;&lt; "Sin " &lt;&lt; deg &lt;&lt; "=" &lt;&lt; "t"
&lt;&lt; setprecision(2) &lt;&lt; result &lt;&lt; endl;

}

//Function Factorial

float factorial(int exponent)

{

int i;

float fact = 1.0;

for(i = 1;i &lt;= exponent;i++)

{

fact = fact * i;

}

return(fact);

}

//Function Menu Definition

int menu()

{

int ch;

while(ch != 3)

{

for(i=0;i &lt; 35;i++)
cout &lt;&lt; "*" ;cout &lt;&lt; endl;

cout &lt;&lt; "Enter the Degree:"; cin &gt;&gt; deg;

for(i=0;i &lt; 35;i++)
cout &lt;&lt; "_" ;cout &lt;&lt; endl;

cout &lt;&lt; "1.SINE SERIES" &lt;&lt; endl;
cout &lt;&lt; "2.COSINE SERIES" &lt;&lt; endl;
cout &lt;&lt; "3.EXIT" &lt;&lt; endl;

for(i=0;i &lt; 35;i++)
cout &lt;&lt; "_" ;cout &lt;&lt; endl;

cout &lt;&lt; " Enter Your Choice:";
cin &lt;&lt; ch;
switch(ch)

{

case 1: compute_sine();
break;

case 2: compute_cosine();
break;

case 3: exit(0);
default:

cout &lt;&lt; "OOPs! Wrong Choice:" &lt;&lt; endl;
break;
}
}
}

// Compute_Cosine function definition

void compute_cosine()

{

n = 11;
result = 0.0;

//Compute the Radian equivalent for the "deg" entered

rad = deg * PI/180;

//Compute the series for Radian measure of Sine

for( i = 0;i &lt; n;i++)

{

exponent = 2 * i;

numa = pow(rad,exponent);

deno = factorial(exponent);

result = result + ((numa * pow(-1,i))/deno);

}

//Display the results

cout &lt;&lt; "Cosine " &lt;&lt; deg &lt;&lt; "t" &lt;&lt; "=" &lt;&lt; "\t" &lt;&lt; setprecision(2) &lt;&lt; result &lt;&lt; endl;

}

## Output

The output of the above program is given below. First you have to test the value of sine series and then verify output of cosine series.

Advertisements