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

更新於:2022年10月13日

290 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告