一、自然级数
代码:
#include<stdio.h>
main()
{
int n;
float t,x,e;
e=1.0;
x=1.0;
n=1;
t=1.0;
while(t>0.000001)
{
t=t*x/n;
e=e+t;
n=n+1;
}
printf("E is %f",e);
getch();
}
二、π级数
代码:
#include<stdio.h>
#include<math.h>
main()
{
double pai=1,p=1,t=1;
int n=2;
while(fabs(t)>0.000001)
{
t=pow(-1.0,n+1)/(2*n-1);
printf("%lf\n",t);
p=p+t;
n=n+1;
}
pai=4*p;
printf("Pai is %lf\n",pai);
}
三、正弦函数级数
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double Factorial(int n) //求阶乘
{
int i=0;
double factorical=1;
if(n==0)
return 1;
for(i=1;i<=n;i++)
factorical=factorical*i;
return factorical;
}
double Mysin(double x)
{
int m = 1, sign = 1;
double t, sum = 0;
while ( fabs(t = sign * pow(x, 2*m -1) / Factorial(2*m - 1)) > 1e-6)
{
sum += t;
++m;
sign *= -1;
}
return sum;
}
int main()
{
double x;
printf("please input x: \n");
scanf("%lf",&x);
printf("%lf\n", Mysin(x));
system("pause");
return 0;
}