Swift 程式計算簡單利息


本教程將討論如何編寫一個 Swift 程式來計算簡單利息。

簡單利息是最常用的計算一定時期內金錢利息的方法。在這種方法中,利息始終按相同的利率應用於原始本金,每個時間段都相同。它是透過將利率乘以本金和時間來計算的。

公式

以下是簡單利息的公式:

S.I = Principal Amount(P) x Time(T) x Rate(R)/100

其中

本金 (P) - 本金代表最初投資的金額。

利率 (R) - 利率代表在給定時間段內應用於本金的利率。

時間 (T) - 時間代表投資金額的年限。

計算簡單利息的演算法

  • 步驟 1 - 定義三個變數(本金、利率和時間)

  • 步驟 2 - 為這些變數賦值

  • 步驟 3 - 實現簡單利息公式(本金 (P) x 時間 (T) x 利率 (R)/100)

  • 步驟 4 - 列印輸出

示例

以下程式演示瞭如何計算簡單利息。

import Foundation import Glibc var principal = 20000 var rate = 3 var time = 5 var SimpleInterest = (principal * time * rate)/100 print("Principal Amount is - ", principal,"rupees") print("Rate of the interest is - ", rate,"%") print("Time period is -", time, "Years") print("\nFinal simple interest is - ", SimpleInterest,"rupees")

輸出

Principal Amount is - 20000 rupees 
Rate of the interest is - 3 % 
Time period is - 5 Years
Final simple interest is - 3000 rupees

在上面的程式碼中,我們使用如下所示的數學公式計算簡單利息:

var SimpleInterest = (principal * time * rate)/100

這裡,本金、利率和時間分別為 20000、3 和 5,因此簡單利息為 3000。

示例

以下程式演示瞭如何使用使用者定義的輸入計算簡單利息。

import Foundation import Glibc print("Please enter the principal amount") var principal = Int(readLine()!)! print("Please enter the interest rate") var rate = Int(readLine()!)! print("Please enter the time period") var time = Int(readLine()!)! var SimpleInterest = (principal * time * rate)/100 print("\nEntered Principal Amount is - ", principal,"rupees") print("Entered Interest Rate is - ", rate,"%") print("Entered Time period is -", time,"Years") print("\nFinal simple interest is - ", SimpleInterest,"rupees")

標準輸入

Please enter the principal amount
10000 
Please enter the interest rate 
4 
Please enter the time period 
5

輸出

Entered Principal Amount is -  10000 rupees
Entered Interest Rate is -  4 %
Entered Time period is - 5 Years

Final simple interest is -  2000 rupees

在上面的程式碼中,我們使用如下所示的數學公式計算簡單利息:

var SimpleInterest = (principal * time * rate)/100

這裡,本金、利率和時間段是使用 readLine() 函式在執行時從使用者處獲取的。因此,給定資料的簡單利息為 3000。

更新於: 2022 年 8 月 4 日

859 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.