我只能用 #include<stdio.h>
来编写这个程序。
我要读系列最高权力'n'
来自用户。
何时 x=45 and n=9
,然后程序给我 0.7068251967
。但是当我使用计算器进行同样的操作时,我得到 0.7068251828
.
我还必须使用递归。
#include<stdio.h>
float pow(float n, int p)
{
if(p == 0)
return 1;
else
return n * pow(n, p-1);
}
int fact(int n)
{
if(n == 0)
return 1;
else
return n * fact(n-1);
}
int main()
{
int n, x, i, sign = 1;
float sum, r;
printf("Enter the angle in degrees.\n");
scanf("%d", &x);
r = 3.14 * x / 180.0;
printf("Enter the odd number till which you want the series.\n");
scanf("%d", &n);
if(n % 2 == 0)
printf("The number needs to be an odd number.\n");
else
{
for(i = 1, sum = 0; i <= n; i += 2, sign *= -1)
{
sum += (sign * pow(r, i)) / fact(i);
}
printf("The sum of the series is %.10f.\n", sum);
}
return 0;
}
最佳答案
我认为原因之一是你将 pi 近似为 3.14。也许你的计算器 考虑更多的 pi 位数。尝试使用更多数字来近似 pi。
关于计算正弦的 C 程序给出的结果不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55302837/