单利主要用于银行业计算资金增长。计算复利的公式是
A = P(1 + r/n)nt
其中 p 代表本金,t 代表年数,r 代表利率,n 代表计息次数。例如,假设
p = 5000
r = 8 => 8/100 => 0.08
t = 4
n = 1
则
A = 5000(1 + (0.08/1))4*1 = 6,802.44
现在将此逻辑应用于 PHP 编程语言。我们接受本金、利率、年数和计息次数的值。然后我们使用公式 P(1 + r/n)nt 计算利息。
在这个程序中,我们使用直接方法计算复利,即使用公式。首先,我们必须从用户那里接受本金、利率、年数和计息次数,并将其赋给变量 p、n、r、t,然后使用公式 P(1 + r/n)nt 并将结果赋给变量 ci。并打印变量 ci 的值作为复利。
算法
步骤 1: 将本金读入变量 p
步骤 2: 将年名义利率(百分比)读入变量 R
步骤 3: 将时间(小数年)读入变量 t
步骤 4: 将计息次数读入变量 n
步骤 5: 将计算值 'R / 100' 赋给变量 r
步骤 6: 将计算值 'p * pow((1 + (r / n)), n * t)' 赋给变量 ci
步骤 7: 打印变量 ci 的值作为复利
<?php
$p = readline("Enter the Principal amount: ");
$R = readline("Enter the Annual nominal interest rate as a percent: ");
$t = readline("Enter the time in decimal years: ");
$n = readline("Enter the number of times the interest is compounded: ");
$r = $R / 100;
$ci = $p * pow((1 + ($r / $n)), $n * $t);
echo "Compound Interest = $ci";
?>
Enter the Principal amount: 10000 Enter the Annual nominal interest rate as a percent: 5 Enter the time in decimal years: 3 Enter the number of times the interest is compounded: 1 Compound Interest = 11576.25