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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP