题目一:
#include<stdio.h>
#include<math.h>
int main()
{
int n = 1;
float sum = 0, term = 1, sign = 1;
while (fabs(term) > 1e-4)
{
term = sign / n;
sum += term;
sign = -sign;
n++;
}
printf("sum=%f", sum);
return 0;
}
题目二:
#include<stdio.h>
#include<math.h>
int main()
{
int n = 1, count = 1;
float sum, term;
float x;
printf("Input x:");
scanf_s("%f", &x);
sum = x;
term = x;
while (fabs(term) > 1e-5)
{
term = -term * x * x / ((n + 1) * (n + 2));
sum += term;
n = n + 2;
count++;
}
printf("sin(x)=%f,count=%d", sum, count);
return 0;
}
题目三:
#include<stdio.h>
#include<math.h>
int main()
{
int count = 0, sign = 1, n = 1;
double sum = 0, term = 1;
while (fabs(term) > 1e-4)
{
term = (float)sign / n;
sum += term;
sign = -sign;
n = n + 2;
count++;
}
printf("pi=%f,count=%d", 4 * sum, count);
return 0;
}
题目四:
#include<stdio.h>
#include<math.h>
int main()
{
int count=1;
double sum = 1, term = 1;
while (fabs(term) > 1e-5)
{
term = term / count;
sum += term;
count++;
}
printf("e=%f,count=%d", sum, count);
return 0;
}