c语言练习——迭代法求sinx的近似值

【方法1】

#include 
#include 
#define PI 3.14159
int main()
{
	int i,flag=1,i2;
	float x, re=0,x2;
	scanf_s("%f", &x);
	x = x * PI / 180;
	for (i=1;i<=100;i+=2)
	{
		**x2 = 1;**
		for (i2 = 1; i2 <= i; i2++)
		{
			x2 *= i2;
		}
		re += flag * (pow(x,i) / x2);
		flag = -flag;
	}
	printf("sin(%f)=%f", x, re);
	return 0;
}

【迭代法】

#include 
#include 
#define PI 3.14159
int main()
{
	int i;
	double x, t, s = 0;
	scanf_s("%lf", &x);
	x = x * PI / 180;
	t = x;
	for (i=1;i<=100;i+=2)
	{
		s += t;
		t = -t * x * x / ((2 * i) * (2 * i + i));
		
	}
	printf("sin(%f)=%f", x, s);
	return 0;
}

你可能感兴趣的:(c)