Kotlin 程序计算复利


在本文中,我们将了解如何计算复利。复利是指对本金和累计利息收取的百分比利息。与单利相比,利率更高。复利的计算公式如下

principle * (Math.pow((1 + rate / 100), time)) – principle

下面是相同的演示

假设我们的输入是

Principle number: 100000
Interest rate: 5
Time period in years: 3

期望的输出将是 -

The Compound Interest is: 15762.50000000001

算法

  • 步骤 1 - 开始

  • 步骤 2 - 声明四个整数值 principalAmount、interestRate、timePeriod、compoundInterest

  • 步骤 3 - 定义 principalAmount、interestRate、timePeriod、compoundInterest 的值。

  • 步骤 4 - 执行 “principle * (Math.pow((1 + rate / 100), time)) – principle” 计算复利并将其存储在 compoundInterest 变量中

  • 步骤 5 - 显示 compoundInterest

  • 步骤 6 - 停止

示例 1

在此示例中,我们将使用上述公式在 Kotlin 中计算复利。首先,声明并设置本金变量 -

val principalAmount = 10000

然后,设置利率和期限变量 -

val interestRate = 5
val timePeriod = 3

现在,使用上述公式计算复利

val compoundInterest = principalAmount.toDouble() * Math.pow((1 +
interestRate.toDouble()/100.00),timePeriod.toDouble())- principalAmount

让我们看看在 Kotlin 中计算复利的示例 -

fun main() { val principalAmount = 10000 println("Principal amount is defined as: $principalAmount") val interestRate = 5 println("The rate of interest is defined as: $interestRate %") val timePeriod = 3 println("The time period is defined as: $timePeriod years") val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble())- principalAmount println("
Compound Interest is: $compoundInterest"
) }

输出

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 3 years
Compound Interest is: 1576.250000000002

示例 2

在此示例中,我们将计算 Kotlin 中的复利

fun main() { val principalAmount = 10000 println("Principal amount is defined as: $principalAmount") val interestRate = 5 println("The rate of interest is defined as: $interestRate %") val timePeriod = 3 println("The time period is defined as: $timePeriod years") getCompoundInterest(principalAmount, interestRate, timePeriod) } fun getCompoundInterest(principalAmount: Int, interestRate: Int, timePeriod: Int) { val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble()) - principalAmount println("
Compound Interest is: $compoundInterest"
) }

输出

Principal amount is defined as: 10000
The rate of interest is defined as: 5 %
The time period is defined as: 3 years
Compound Interest is: 1576.250000000002

更新于: 2022-10-13

417 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告