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 converge 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 the 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.

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

## Flowchart – Sine and Cosine Series

## 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 <iostream.h>
#include <stdlib.h>
#include <cmath>
#include <stdio.h>
#include <conio.h>
#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< n;i++) {
exponent = (2 * i) + 1;
numa = pow(rad,exponent);
deno = factorial(exponent);
result = result + ((numa * pow(-1,i))/deno);
}
//Display the results
cout << "Sin " << deg << "=" << "t"
<< setprecision(2) << result << endl;
}
//Function Factorial
float factorial(int exponent)
{
int i;
float fact = 1.0;
for(i = 1;i <= exponent;i++)
{
fact = fact * i;
}
return(fact);
}
//Function Menu Definition
int menu()
{
int ch;
while(ch != 3)
{
for(i=0;i < 35;i++)
cout << "*" ;cout << endl;
cout << "Enter the Degree:"; cin >> deg;
for(i=0;i < 35;i++)
cout << "_" ;cout << endl;
cout << "1.SINE SERIES" << endl;
cout << "2.COSINE SERIES" << endl;
cout << "3.EXIT" << endl;
for(i=0;i < 35;i++)
cout << "_" ;cout << endl;
cout << " Enter Your Choice:";
cin << ch;
switch(ch)
{
case 1: compute_sine();
break;
case 2: compute_cosine();
break;
case 3: exit(0);
default:
cout << "OOPs! Wrong Choice:" << 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 < n;i++)
{
exponent = 2 * i;
numa = pow(rad,exponent);
deno = factorial(exponent);
result = result + ((numa * pow(-1,i))/deno);
}
//Display the results
cout << "Cosine " << deg << "t" << "=" << "\t" << setprecision(2) << result << 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.