用c语言编写函数求表达式1-1⼀2+1⼀3-1⼀4+1⼀5-1⼀6+1⼀7-…+1⼀n的值

详细些!
2024-12-21 08:52:34
推荐回答(2个)
回答1:

#include
main()
{
int n,sign=1,i;
double sum=0.0;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=sign*1.0/i;
sign=-sign;
}
printf("sum=%lf\n",sum);
}

回答2:

#include
void main()
{
int n,i;
float sum=0.0;
printf("Please enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==1) sum+=1.0/i;
else sum-=1.0/i;
}
printf("sum=%lf\n",sum);
}