这太令人沮丧了。我无法在任何地方找到这个答案,我无法自己弄清楚。这是在大学课堂上的任务。我们应该有一个结果:复利利率公式
10年,每月存入$ 100增长到$ 17793.03
我如何计算这个用C++?
这太令人沮丧了。我无法在任何地方找到这个答案,我无法自己弄清楚。这是在大学课堂上的任务。我们应该有一个结果:复利利率公式
10年,每月存入$ 100增长到$ 17793.03
我如何计算这个用C++?
A = P (1 + r/n)^nt
这是每年复利复利的公式。
A = the future value of the investment/loan, including interest
P --> Principal amount (The starting amount)
r --> rate (%)
n --> the number of times that interest is compounded per year
t --> t = the number of years the money is invested or borrowed for
使用上述公式并替换值,让计算机完成剩下的工作。 我希望这可以帮助你。我只是一个初学者。这是我认为我会这样做的方式。
编辑
抱歉,我前面提到的公式是不正确这样的问题。 此正确的公式: -
PMT * (((1 + r/n)^nt - 1)/(r/n))
PMT--> Principal amount deposited monthy (100 $)
其余部分的值保持相同。试试这个,并把值存储为double。这应该工作。我在代码块中试过。小心数值和括号。
希望这会有所帮助。
#include <iomanip>
#include <iostream>
int main()
{
auto balance = 0.0;
for (auto i = 0; i < 120; ++i)
balance = balance * (1 + 0.075/12) + 100;
std::cout << std::setprecision(7) << balance << '\n';
}
经常性存款公式可以作为
M = R * ((1+r/p)^n-1)/((1+r/p) -1) = R * p/r * ((1+r/p)^n-1)
M is Maturity value
R is deposit amount
r is rate of interest
p is the number of parts of the year that is used, i.e., p=4 for quarterly and p=12 for monthly,
n is the number of payments, i.e., the payment schedule lasts n/p years, and then r is the nominal annual interest rate, used in r/p to give the interest rate over each part of the year
的工作代码如下:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double r = 7.5/100;
int n = 120;
int p = 12;
int R = 100;
#if 0
double result = 0;
for(int i = 0 ; i < n ; ++i)
{
result *= (1 + r/p) ;
result += R;
}
#else
double result = R * (p/r)* (pow((1+r/p), n) - 1);
#endif
std::cout<<result;
return 0;
}
你需要找到一个公式,然后在C++ – Slava
什么利率执行它? – roottraveller
以下是Google电子表格中的答案:https://docs.google.com/spreadsheets/d/1wi8_m1nQWtXhzG6dHFRUeT36vRGO3n5mAG5gAKUNWdI/edit?usp=sharing如果您使用该电子表格中的数字(每年7.5%的利率,每月复合)和公式,你很快就会知道如何用C++编写代码。 –