- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to calculate the value of sin(x) and cos(x)
Given with the input as angles and the task is to calculate the value of sin(x) and cos(x) corresponding to the given angle and display the result
For Sin(x)
Sin(x) is a trigonometric function which is used to calculate the value of x angle.
Formula
$$\sin (x) = \displaystyle\sum\limits_{k=0}^\infty \frac{(-1)^{k}}{(2k+1)!}x^{2k+1}$$
For Cos(x)
Cos(x) is a trigonometric function which is used to calculate the value of x angle.
Formula
$$\cos (x) = \displaystyle\sum\limits_{k=0}^\infty \frac{(-1)^{k}}{(2k)!}x^{2k}$$
Approach used in the below program is as follows −
- Input the value of x angle for sin(x) and cos(x)
- Apply the formulas given for sin(x) and cos(x)
- Print the result
ALGORITHM
START Step 1-> declare function to calculate value of sin void cal_sin(float n) declare and set float acc = 0.0001, denominator, sinx, sinval Set n = n * (3.142 / 180.0) Declare float temp = n Set sinx = n Set sinval = sin(n) Declare and set int i = 1 DO set denominator = 2 * i * (2 * i + 1) set temp = -temp * n * n / denominator Set sinx = sinx + temp Set i = i + 1 While(acc <= fabs(sinval - sinx)) print sinx Step 2-> Declare function to calculate value of cos void cal_cos(float n) Declare and set float acc = 0.0001, temp, denominator, cosx, cosval Set n = n * (3.142 / 180.0) Set temp = 1 set cosx = temp set cosval = cos(n) Set int i = 1 Do set denominator = 2 * i * (2 * i - 1) Set temp = -temp * n * n / denominator Set cosx = cosx + temp Set i = i + 1 While(acc <= fabs(cosval - cosx)) print cosx Step 3-> In main() Declare float n = 30 Call cal_sin(n0 set n=60 Call cal_cos(n) STOP
Example
#include <iostream> #include <math.h> using namespace std; //calculate value of sin void cal_sin(float n) { float acc = 0.0001, denominator, sinx, sinval; n = n * (3.142 / 180.0); //convert in radian float temp = n; sinx = n; sinval = sin(n); int i = 1; do { denominator = 2 * i * (2 * i + 1); temp = -temp * n * n / denominator; sinx = sinx + temp; i = i + 1; } while (acc <= fabs(sinval - sinx)); cout<<sinx; } //calculate value of cos void cal_cos(float n) { float acc = 0.0001, temp, denominator, cosx, cosval; n = n * (3.142 / 180.0); //convert in radiam temp = 1; cosx = temp; cosval = cos(n); int i = 1; do { denominator = 2 * i * (2 * i - 1); temp = -temp * n * n / denominator; cosx = cosx + temp; i = i + 1; } while (acc <= fabs(cosval - cosx)); cout<< cosx; } int main() { float n = 30; cout<<"value of Sin is : "; cal_sin(n); cout<<"\n"; n=60; cout<<"value of Cos is : "; cal_cos(n); return 0; }
Output
value of Sin is : 0.500061 value of Cos is : 0.499847
- Related Articles
- Find the value of ( x ) in each of the following:( sqrt{3} sin x=cos x )
- Find the value of ( x ) in each of the following:( cos 2 x=cos 60^{circ} cos 30^{circ}+sin 60^{circ} sin 30^{circ} )
- Find the value of ( x ) in each of the following:( tan x=sin 45^{circ} cos 45^{circ}+sin 30^{circ} )
- Find the value of ( x ) in each of the following:( sqrt{3} tan 2 x=cos 60^{circ}+sin 45^{circ} cos 45^{circ} )
- C Program for sum of cos(x) series
- In triangle $ABC$, right-angled at $B$, if $tan A = frac{1}{sqrt3}$, find the value of:(i) $sin A cos C + cos A sin C$(ii) $cos A cos C - sin A sin C$
- If ( sin theta+cos theta=x ), prove that ( sin ^{6} theta+cos ^{6} theta=frac{4-3left(x^{2}-1right)^{2}}{4} )
- If ( sin x=frac{6 sin 30^{circ}-8 cos 60^{circ}+2 tan 45^{circ}}{2left(sin ^{2} 30^{circ}+cos ^{2} 60^{circ}right)} ), then ( x ) is equal to
- If ( sin (A-B)=sin A cos B-cos A sin B ) and ( cos (A-B)=cos A cos B+sin A sin B ), find the values of ( sin 15^{circ} ) and ( cos 15^{circ} ).
- Find the value of ( x ) in each of the following:( 2 sin frac{x}{2}=1 )
- Find the value of ( x ) in each of the following:( quad 2 sin 3 x=sqrt{3} )
- If $cos theta_{1} +cos theta_{2}+cos theta_{3}+cos theta_{4}+cos theta_{5} = 5$, find the value of $sin theta_{1} +sin theta_{2}+sin theta_{3}+sin theta_{4}+sin theta_{5}$.
- If $sintheta-costheta=0$, then find the value of $sin^{4}theta+cos^{4}theta$.
- Write a program to calculate pow(x,n) in C++
- If $frac{x}{a}costheta+frac{y}{b}sintheta=1$ and $frac{x}{a}sintheta-frac{y}{b}costheta=1$, prove that $frac{x^{2}}{a^{2}}+frac{y^{2}}{b^{2}}=2$.

Advertisements