November 8th, 2012, 04:28 AM
Write a C++ program to compute Sin(x)
Hi. I'm having problem writing this program as my assignment. I've spent hours on this but still i can't get the right answer.
Here is the question:
Write a C++ program to compute Sin(x) where
x x^3 x^5 x^7 x^9 x^n
sin (x) = ----- ─ ---- + ---- ─ ---- + ----- ─ …………. ------
1! 3! 5! 7! 9! n!
Your program should accept two values from the user (the angle x and the value of n) and then should compute and print the value of sin(x).
To make the program, do following tasks.
• Write two functions, i.e. function to calculate factorial and function to calculate power having following prototypes.
double Factorial (int n); //Factorial function prototype
double Power(double x, int y); //Power function prototype
• Use these functions in your main function to compute the series.
Till now, I've written the following program but I am not able to get the right answer.
using namespace std;
double fact (int f); //declaration of factorial function
double power(double x, int y); //declaration of power function
int x=0; //value of x in the series
float sum_pos = 0;
cout << "Enter the value of x: " << endl;
cin >> x;
for (int i=1; i<=1000; i+=4)
sum_pos = sum_pos + (power (x,i) / fact (i));
for (int i=3; i<=1000; i+=4)
sum_neg = sum_neg + (power (x,i) / fact (i));
t_sum = sum_pos - sum_neg;
cout << "Sin of " << x << " = " << t_sum << endl;
//Function for Factorial
double fact (int x)
for (int i=1; i<=x; i++)
//Function for Power
double power (double x, int y)
for (int i=1; i<=y; i++)
I have to submit it this weekend. So please help me what am i doing wrong??
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!