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 converge into a single value.

Advertisements

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

Sine Series
Sine Series
Cosine Series
Cosine Series

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

Advertisements

= π/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.

Flowchart – Sine and Cosine Series

Flowchart - Sine and Cosine Series in C++
Flowchart – Sine and Cosine Series in C++

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.

Output - Sine Series
Output – Sine Series
Output - Cosine Series
Output – Cosine Series

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.