下面是用C语言编写的简易房贷月供计算器,供参考。
#include "stdio.h"
#include "math.h"
int main()
{
double amountOfLoan,annualInterest,repaymentPeriod; //input
double monthlyPayments; //output
double monthlyInterest,numOfMonth,tmp; // intermediate variables
//Input
printf("\n A simple loan calculator\n\n");
printf(" Please input amount of loan(¥):");
scanf("%lf",&amountOfLoan);
printf("\n Please input annual interest(%%):");
scanf("%lf",&annualInterest);
printf("\n Please input repayment period(years):");
scanf("%lf",&repaymentPeriod);
//calculate
monthlyInterest = annualInterest/100/12;
numOfMonth = repaymentPeriod*12;
tmp = pow(1+monthlyInterest,numOfMonth);
monthlyPayments = (amountOfLoan * tmp * monthlyInterest)/(tmp-1);
//output
printf("\n\n You need to repay ¥%.2f each month. Good luck!\n",monthlyPayments);
return 0;
}
下面是用HTML、CSS和Javascript脚本语言写的一个简易贷款月供计算器,可以在浏览器中运行。HTML定义了网页元素,CSS定义了页面样式,Javascript实现了核心的计算部分。Javascript片段(标签之间的代码)的实现思路和上面写的C语言代码是相似的:先获取数据,然后借助数学库函数进行计算,最后输出结果。不同的是,这里从网页上的文本框获取数据,结果也是显示到页面元素上。
JavaScript Loan Calculator
JavaScript Loan Calculator
Amount of the loan(¥):
Annual interest(%):
Repayment period(years):
Approximate Payments:
Monthly Payments:
¥
Total Payments:
¥
Total Interest:
¥
把上述代码复制到一个文本文档中,后缀名改为.html,双击即可在浏览器中运行。