這太令人沮喪了。我無法在任何地方找到這個答案,我無法自己弄清楚。這是在大學課堂上的任務。我們應該有一個結果:複利利率公式
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++編寫代碼。 –