How do i code this?

sin(x) = x^1/1! - X^3/3! + x^5/5! - x^7/7! + x^9/9...

this is the sine i have but how to i do using for-loop?

//sin(x) = x^1/1! - X^3/3! + x^5/5! - x^7/7! + x^9/9!..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <stdio.h>
#include <math.h>
	void main ()
{
	// x = The Input of sin(x)
	// sine is the 'Total of sin (x)
	//I - For-loop
	float x,sine;
		int I;
			printf("Please Input The x Value");
			scanf("f",&x);//Determind the user input
			for(I=1; I<=x;)
				sine= pow(1,1!);// here pow is red colour as well as !
				printf("Sine Of %f = %f",x, sine); 
	}
x^n translates to pow( x, n )

n! translates to fac( n ). Note that C++ does not have a ! as a factorial operator. Nor does it have a factorial function in math.h. You'll have to write fac() yourself. Considering that it is the most common example program, I think you'll manage to write (find) it.

Now there are several ways to write this. The simplest one would be to have two for loops : one for adding and one for subtracting. The first would start from 1, the second - from 3. Both would increment by 4.

If you still don't understand something, ask specific questions. Also, you might want to read a tutorial..

When you do write this, you might want to notice that it is wasteful to recalculate the factorial and the power for each term. You know what the ratio of two consecutive terms is, so you can make this a lot faster.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//sin(x) = x^1/1! - X^3/3! + x^5/5! - x^7/7! + x^9/9!..



#include <stdio.h>
#include <math.h>
int fact(int x);
float sine(float x);

int main() // main is here
{ // Start of main block
	int num;
	float x,sine;
		int I;

	printf("Please Enter a Value For (x) Degree");
	scanf("%d",&x);// degree
	{

	
	}
} // end of main block

int fact (int x)
	  float sine(float x)
{ // start of fact() block
	// int = ans =1;
	int ans = 1;
		    float sine; 
		int I,J,Q,K,num;
	for (; x>1; x=x-1)
	{

		ans = ans *x;
	}
	    float sine; 
		int I,J,Q,K;
		for(I=1;I<=x;I+4)
			for(J=3;J<=x;J+4)
				Q= pow( x, I  *fact(I));
                K= pow( x, J  *fact (J));

				sine = pow( x, I )-pow( x, J );
                                                               sine ((param*PI/180);
				printf("sine",&sine);


  }
[This is wad i have done and i cant run ]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <math.h>
int fact(int x);
float sine(float x);

int main()
{
	int num;
	float x,sine;
		int I;

	printf("Please Enter a Value For (x) Degree");
	scanf("%d",&x);
	{//why is this here ?
               //why isn't there anything here ?
	
	}
}

int fact (int x)//here you say that you're starting fac() implementation
	  float sine(float x)//here you say that you're starting sine() implementation
{//so what does this one mean?
	int ans = 1;
		    float sine;
		int I,J,Q,K,num;
	for (; x>1; x=x-1)
	{

		ans = ans *x;//this calculates a factorial ...
	}
	    float sine; //so what is this part supposed to do ?
                               //why would you calculate sine in fac() function?
                               //also, you already declared sine above..
		int I,J,Q,K;
		for(I=1;I<=x;I+4)
			for(J=3;J<=x;J+4)
				Q= pow( x, I  *fact(I));//this just overwrites Q a bunch of times
                K= pow( x, J  *fact (J));

				sine = pow( x, I )-pow( x, J );
				printf("sine",&sine);//this is supposed to be in main()


  }

And your formatting is horrible.
This task seems to be too big for you. Start from the basics. Write a function fac() and a program that would calculate the factorial of user input. Again, you might want too look into some tutorial..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
int fact(int x);

int main() // main is here
{ // Start of main block
	int num;
	printf("Please Enter a number");
	scanf("%d",&num);
	if(num<0){
		printf(" No negative numbers, please.\n");
	} 
	else 
	{ 
		if (num>15)
		{
			printf( "Too large\n");
		}
		else
		{
			printf ("%d fact = %d\n",num, fact(num));
		}
	}
	return 0; 
} 

int fact (int x)
{ // start of fact() block
	// int = ans =1;
	int ans = 1;
	for (; x>1; x=x-1)
	{
		ans = ans *x;
	}
	return ans;
}
Last edited on
The Function For Fact Is Here but i have no idea how to put sin with fact together :( helppy :(
bumps
I think you need to take a step back and work out how you calculate the next term in the series from the current one.

You're making the poor computer do a lot of extra work!

Andy
@Frozendog, now write a function which calculates the nth term in the sum. You know how to exponentate and how to find the factorial. Notice how constants change with n. If n starts from 0,
0 -> 1
1 -> 3
2 -> 5
3 -> 7
Also, note that terms are negative when n is odd. Your function will be double term(double x, int n);

@andywestken, he's having enough problems with the brute approach. I think it would be better if he did this first.
@hamsterman Point taken!

(I do thing that the recursive approach is simpler, but probably only once you have a good handle on recursion...)
Last edited on
Topic archived. No new replies allowed.