当前位置:网站首页>C语言例题-计算常量e的值
C语言例题-计算常量e的值
2022-08-05 11:49:00 【Blue_lan18】
编写程序,用下面的公式计算e的近似值:1 + 1/1! + 1/2! + 1/3! +…+ 1/n!,这里n是用户输入的整数。
# include <stdio.h>
int main()
{
int i, n;
double e1 = 1, e2, e3 = 0, e = 0;
printf("Enter n: ");
scanf("%d", &n);
for(i = 1; i <= n; i++){
e1 *= i;//计算分子部分
printf("e1 = %.6f\n", e1); //为了便于理解,显示计算过程
e2 = 1.0 / e1;
printf("e2 = %.6f\n", e2);
e3 += e2;//把所有分数部分相加
printf("e3 = %.6f\n", e3);
printf("\n");
}
e = 1 + e3;
printf("e = 1 ");
for(i = 1; i <= n; i++){
printf("+ 1/%d ! ", i);
}
printf("=%.6f\n", e);
return 0;
}
当n的值够大,e的值就越精确,例如,当n为100时,e为2.718282。
边栏推荐
- Gao Zelong attended the Boao Global Tourism Ecology Conference to talk about Metaverse and Future Network Technology
- Security Issues and Prevention in Web3
- 动手学深度学习_GoogLeNet / Inceptionv1v2v3v4
- .NET in-depth analysis of the LINQ framework (6: LINQ execution expressions)
- Gray value and thermal imaging understanding
- 【硬件架构的艺术】学习笔记(3)处理多个时钟
- Machine Learning - Ensemble Learning
- Web3 中的安全问题和防范
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- power failure...Trouble trouble trouble!!!
猜你喜欢

软件设计七大原则之开闭原则(Open-Closed Principle, OCP)

5G NR system messages

Web3 中的安全问题和防范

灰度值与热成像理解

解决2022Visual Studio中scanf返回值被忽略问题

对于聚合物聚乙二醇PEG大家了解多少了?以及在生活中的应用

内存问题难定位,那是因为你没用ASAN

D-Desthiobiotin-PEG4-Maleimide主要物理性质特点 !

Support Vector Machine SVM

163_Tricks_Power BI one-click batch creation of custom field parameters
随机推荐
Go编译原理系列9(函数内联)
常见的 web 安全问题总结
一张图理解EOS是什么
前沿技术数字孪生如何应用在智慧城市上?
Cesium.js 地形挖洞
Hands-on Deep Learning_GoogLeNet / Inceptionv1v2v3v4
平安萌娃卡保险怎么样?让父母读懂几个识别产品的方法
普通二本毕业八年,京东就职两年、百度三年,分享大厂心得
swig 语法介绍
冬日里,28℃的爱情
knife4j
Introduction to the Evolution of Data Governance System
hdu1455 Sticks(搜索+剪枝+剪枝+.....+剪枝)
795. 前缀和
Three.js 点击模型,高亮发光模型外轮廓
365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
澳洲站:电吹风AS/NZS 60335.2.23: 2017 安全标准测试
5G NR system messages
Naive bayes
Mathcad 15.0软件安装包下载及安装教程