C ,兴趣计算器未显示正确的输出
C++, Interest Calculator not displaying correct output
询问"编写一个读取初始投资余额和利率的程序,然后打印投资达到一百万美元所花费的年数。/p>
- 我投入的输入为100,而利率为3。但是,当我编译和运行时,输出为29,这是不正确的,因为金额仅为187,完全不接近一百万。
/*
Question: Write a program that reads an initial
investment balance and an interest rate, then
prints the number of years it takes for the
investment to reach one million dollars.
*/
#include <iostream>
using namespace std;
int main()
{
//Obtain user amount
double amount;
cout << "Please enter an initial investment balance ($0.00): $";
cin >> amount;
//Obtain user interest rate
double interest_rate;
cout << "Please enter an interest rate: ";
cin >> interest_rate;
//Convert interest rate to decimal
interest_rate = interest_rate / 100;
int time = 1;
//Calculate how many years
while (amount < 1000000)
{
amount = amount * (1 + (interest_rate * time));
++time;
}
//Display years
cout << "Years to reach one million: " << time;
return 0;
}
我期望的输出是:
"达到一百万的年:333300&quot"
由于333300正好是一百万。
在一年内,金额将增长到
amount * (1 + interest_rate)
和两年后,金额增长到
amount * (1 + interest_rate) * (1 + interest_rate)
假设您的利率年度复合。您包含time
,以及amount
的连续乘法是错误。
请注意,有一个封闭的形式解决方案。对于速率 r ,初始数量 i ,最终数量 a ,年数 t 是
t = ln( a / i (/ln(1 r (
您需要汇总。
相关文章:
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 输出显示多个带整数的字符串值
- 如何使代码的输出显示为"工资减税"
- 如何让我的输出显示输入金额的星号
- 控制台输出显示ASCII值,而不是数字的预期数字
- 如何修复输出显示,以便显示正确排序的数据
- Android Studio 3.2:cmake message()输出显示在哪里
- 如何将OpenCV的输出显示或流式传输到HTML页面或其他一些客户端应用程序
- 我写了一个函数来删除循环链接列表中的第 1 个节点,但输出显示无限次"55 44 33 22 11 99",那么我该如何解决呢?
- 从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
- 为什么我的输出显示显示计算器中的每个选项
- C++将输出显示到php中
- 从 PHP 调用 C++ 应用程序 - 输出显示"Array"
- 有没有办法C++让循环的输出显示在同一行上
- 为什么日志输出显示在控制台主机中,而不显示在ISE中?(v4.0)
- 如何让GDB为每一行输出显示时间戳
- 为什么输出显示为无符号数
- 如何获得gdb tui汇编输出显示指令
- 为什么输出显示错误
- FFTW输入输出显示DSP