求助 C语言程序调试题~谢谢!!!

2024-12-22 01:23:30
推荐回答(3个)
回答1:

#include
void main(){
double term, result = 1/*result 没有赋初值,会默认为0,怎么相乘都是0*/;
int n,m;
printf("Enter m: ");
scanf("%d",&m);
for (n=2; n<=m; n+2)
{
/*term = (n*n)/((n-1)*(n+1));这里 n 是整数 n*n是整数 ((n-1)*(n+1))也是整数 默认整除 */ term = (n*n*0.1/*将n*n变成float形*/)/((n-1)*(n+1));
result = result *term;
}
printf("result = %f\n", 2 * result); /*不知道为什么*2 就不管这里了*/
getchar();
}

回答2:

#include
void main()
{ double term, result=1.0;/***/
int n,m;
printf("Enter m: ");

scanf("%d",&m);

for (n=2; n<=m; n+=2) /***/

{
term = (double)n; /***/
term = (term*term)/((term-1)*(term+1)); /***/

result = result *term;

}

printf("result = %f\n", 2 * result);

getchar();getchar();/*输入数时有一个回车*/
}
/*计算π/2的级数=2^2/(1x2)+4^2/(3x5)+6^2/(5x7)...,收敛很慢,n=1000,才求得π≈3.14*/

回答3:

#include
void main()
{ double term, result; //result=1;
int n,m;
printf("Enter m: ");
scanf("%d",&m);
for (n=2; n<=m; n+2) //n+=2
{ term = (n*n)/((n-1)*(n+1));
result = result *term;
}
printf("result = %f\n", 2 * result);
getchar();
}