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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.