计算免息分期的收益。

今天有朋友问到他买的手机分几期合适,于是写了代码帮他计算免息分期具体能带来多少收益。
比如,10000的手机,选择24期免息分期,把这些钱买成银行年利率3.9%的理财,那么相比较于不分期,两年的收益率是多少。
上代码:

#include
#include
using namespace std;
double CalcEarning(int periods, double totalAmount, double depositAnnualRate, double installmentRateofEachPeriod = 0.0)
{
	double totalEarning = 0.0;
	double curAmount = totalAmount;
	double curMonthEarning = 0.0;
	for (int i = 0; i < periods; i++)
	{
		curMonthEarning = curAmount * depositAnnualRate / 12;
		curAmount -= totalAmount / periods;					 
		totalEarning += curMonthEarning;
	}
	return totalEarning;
}

void main()
{
	cout << CalcEarning(24, 10000, 0.039) << endl;	  //406.25
	cout << CalcEarning(12, 10000, 0.039) << endl;	  //211.25
	cout << CalcEarning(6, 10000, 0.039) << endl;	  //113.75
	cout << CalcEarning(3, 10000, 0.039) << endl;	  //65
	system("pause");
}

代码很简单,可直接编译,参数名和语句应该好懂吧。
输出就是收益。

朋友是学会计的,说跟年金复利现值终值这些概念很像,顺便研究了一下。
整理下知识碎片:
按照年利率10%,3年期表示.
1.复利现值:
200*(P/F,10%,3)
希望在三年后取出200万元,那么今天应该存入多少钱?
2.复利终值:
200*(F/P,10%,3)
这个很好理解,现在存入200万元,三年后值多少钱。
3.年金现值
我是租客,每年付1w元给房东,租三年,我准备现在一次性凑齐然后放在银行里边买着理财边付房租,那么我需要准备多少钱。
4.年金终值
我是房东,把房子租出去三年,我每年会收到1w元房租,那么三年后我会有多少钱。
这个其实算的是期初年金终值,因为房租会在每年年初收到。
如果是每年年末才收到房租,那算出来就是期末年金终值 。

你可能感兴趣的:(代码解决身边事)