Kotlin程序计算简单利息
在本文中,我们将了解如何在Kotlin中计算简单利息。它是总本金的百分比利息。与复利相比,收益较低。简单利息的计算公式为
(principle*rate*time)/100
下面是相同的演示
假设我们的输入是
Principle number: 100000 Interest rate: 5 Time period in years: 2
期望输出为
Simple Interest: 1000
算法
步骤1 − 开始
步骤2 − 声明四个整数值principalAmount(本金)、interestRate(利率)、timePeriod(期限)、simpleInterest(利息)
步骤3 − 定义principalAmount、interestRate、timePeriod和simpleInterest的值
步骤4 − 执行“(principle*rate*time)/100”计算简单利息,并将其存储在simpleInterest变量中
步骤5 − 显示simpleInterest
步骤6 − 结束
示例1
在这个例子中,我们将使用上面给出的公式在Kotlin中计算简单利息。首先,声明并设置本金变量
val principalAmount = 10000
然后,设置利率和期限变量:
val interestRate = 5 val timePeriod = 3
现在,使用上述公式根据本金、利率和期限计算简单利息:
val simpleInterest = (principalAmount*interestRate*timePeriod)/100
让我们现在看看在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 = 2 println("The time period is defined as: $timePeriod years") val simpleInterest = (principalAmount*interestRate*timePeriod)/100 println("
Simple Interest is: $simpleInterest") }
输出
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
示例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 = 2 println("The time period is defined as: $timePeriod years") getSimpleInterest(principalAmount, interestRate, timePeriod) } fun getSimpleInterest(principalAmount: Int, interestRate: Int, timePeriod: Int) { val simpleInterest = (principalAmount*interestRate*timePeriod)/100 println("
Simple Interest is: $simpleInterest") }
输出
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
广告