Find and display the values of both sides of the following mathematical series expression and
an absolute difference of both sides. User can input either of angles in degree 90, 60, 30 etc.
sin(x) = x - ((x^3)/3!)+((x^5)/5!)-((x^7)/7!)+((x^9)/9!)
Once the user enters the angle in degrees. Your program should do the following (Write separate
functions for a, b, c, and d)
a) LHS Result
b) RHS Result
c) Difference
d) First term, series of two terms, series of three terms, series of four terms, series of five terms.
#include <iostream>
using namespace std;
double sin30 = 0.5;
double sin60 = 0.866;
double sin90 = 1.0;
int fact(int n) {
if (n == 1) return 1;
else return n * fact(n - 1);
}
int pow(int n, int m) {
int res = 1;
for (int i = 1; i <= m; i++)
res = i * res;
return res;
}
double sin(int x) {
return x - (1.0 * pow(x, 3) / fact(3))
+ (1.0 * pow(x, 5) / fact(5))
- (1.0 * pow(x, 7) / fact(7))
+ (1.0 * pow(x, 9) / fact(9));
}
int main() {
cout << "lhs: " << sin30 << ", rhs: " << sin(30) << ", diff: " << sin30 - sin(30) << endl;
cout << "lhs: " << sin60 << ", rhs: " << sin(60) << ", diff: " << sin60 - sin(60) << endl;
cout << "lhs: " << sin90 << ", rhs: " << sin(90) << ", diff: " << sin90 - sin(90) << endl;
return 0;
}
Comments
Leave a comment