您的位置:首页 > 编程语言 > C语言/C++

c语言 分数阶乘计算

2017-10-13 11:54 302 查看
#include <stdio.h>
#include <stdlib.h>
#include <math.h>                                                             //数学头文件,里面含有pow函数
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//计算n为1~100的[(-1)^(n+1)*(1/n)]!
int main(int argc, char *argv[]) {
double jiechenghe=0,n=1;                                              //定义double型阶乘和
for(n;n<=100;n++)                                                     //分别计算n从1~100的数
{
jiechenghe+=pow(-1,n+1)*(1.0/n);                              //阶乘和等于n从1~100的数之和,调用pow()函数,实现计算
}
printf("n为1~100的[(-1)^(n+1)*(1/n)]!为%.4lf",jiechenghe);    //输出阶乘和,double型精确到小数后6为,因为100+的缘故,精确度最高是4
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: