编写C语言程序,计算s=1+1/x+/x^2+1/x^3……(x>1)的近似值,使其误差小于10的-6次方
时间: 2024-03-11 20:29:32 浏览: 77
使用C语言幂级数求近似值
#include <stdio.h>
#include <math.h>
int main()
{
double x, s, term;
int n;
printf("Please enter a value of x greater than 1: ");
scanf("%lf", &x);
s = 1.0;
term = 1.0;
n = 1;
while (term >= 1e-6) {
term *= 1.0 / x;
s += term;
n++;
}
printf("s = %.6f\n", s);
printf("The number of terms required is %d\n", n);
return 0;
}
阅读全文