Kotlin程序计算单利和复利


在这篇文章中,我们将从用户那里输入本金、利率和时间(年),以计算单利和复利:

  • 单利 - 对总本金的百分比利息。与复利相比,收益较低。

  • 复利 - 对本金和累积利息收取的百分比利息。利率高于单利。

以下是演示:

假设我们的输入是:

Principal = 25000.0
Annual Rate of Interest = 10.0
Time (years) = 4.0

期望的输出是:

Simple Interest: 10000.0
Compound Interest: 11602.500000000007

算法

  • 步骤 1 - 开始

  • 步骤 2 - 声明 5 个整数:principalAmount(本金)、interestRate(利率)、timePeriod(期限)、simpleInterest(单利)和 compoundInterest(复利)

  • 步骤 3 - 计算 (p * r * t)/100

  • 步骤 4 - 计算 p * Math.pow(1.0+r/100.0,t) - p;

  • 步骤 5 - 将步骤 4 的输出存储到 simpleInterest 中

  • 步骤 6 - 将步骤 5 的输出存储到 compoundInterest 中

  • 步骤 7 - 显示 simpleInterest

  • 步骤 8 - 显示 compoundInterest

  • 步骤 9 - 结束

示例 1

在这个例子中,我们将使用广泛使用的公式计算单利和复利。首先,声明一个变量表示本金

val principalAmount = 10000

现在,声明利率和期限:

val interestRate = 5
println("The rate of interest is defined as: $interestRate %")

val timePeriod = 2
println("The time period is defined as: $timePeriod years")

使用公式计算单利和复利

val simpleInterest = (principalAmount*interestRate*timePeriod)/100
println("
Simple Interest is: $simpleInterest") val compoundInterest = principalAmount.toDouble() * Math.pow((1 + interestRate.toDouble()/100.00),timePeriod.toDouble()) - principalAmount println("
Compound Interest is : $compoundInterest")

现在让我们看看计算单利和复利的完整示例:

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 = 2 println("The time period is defined as: $timePeriod years") val simpleInterest = (principalAmount*interestRate*timePeriod)/100 println("
Simple Interest is: $simpleInterest"
) 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: 2 years
Simple Interest is: 1000
Compound Interest is: 1025.0

示例 2

在这个例子中,我们将计算单利和复利:

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") getInterest(principalAmount, interestRate, timePeriod) } fun getInterest(principalAmount: Int, interestRate: Int, timePeriod: Int) { val simpleInterest = (principalAmount*interestRate*timePeriod)/100 println("
Simple Interest is: $simpleInterest"
) 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

 Simple Interest is: 1500
Compound Interest is: 1576.2500000000018

更新于:2022年10月13日

650 次浏览

启动你的职业生涯

完成课程获得认证

开始
广告