|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 我爱橙 于 2022-5-2 16:34 编辑
输入银行定期存款的年利率rate,存款期n年,存款本金capital,试编程计算并输出n年后的本利之和deposit。程序中所有浮点数的数据类型均为double类型。
提示:
从键盘输入数据可以使用函数scanf()。本例中为scanf("%lf,%d,%lf", &rate, &n, &capital);
本程序最终计算的是复利。
计算幂的数学函数为pow(a,n), 代表a的n次幂。
使用数学函数,需要在程序开头加上编译预处理指令 #include <math.h>
以下为程序的一个运行示例:
Please enter rate, year, capital:
0.0225,10,1000↙
deposit=1249.203
- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int n;
- double a,rate,capital,deposit;
-
- printf( "Please enter rate, year, capital:\n");
- scanf("%lf,%d,%lf", &rate, &n, &capital);
- a=pow(rate,n);
- deposit=capital*(1+a);
- printf("deposit=%.3f\n",deposit);
- return 0;
- }
复制代码
为什么实现不了程序功能,结果一直是0?
本帖最后由 isdkz 于 2022-5-2 16:11 编辑
一个小于 0 的小数的幂只会越来越小,
a 本就是一个很小的小数,你再给它来个几次方就趋近于 0 了,
所以你对年利率的计算有点误解- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int n;
- double a,rate,capital,deposit;
-
- printf( "Please enter rate, year, capital:\n");
- scanf("%lf,%d,%lf", &rate, &n, &capital);
- a=pow(1 + rate,n); // 改了这里
- deposit=capital* a; // 改了这里
- printf("deposit=%.3f\n",deposit);
- return 0;
- }
复制代码
|
|