如何使用Golang程式碼計算單利?


在本教程中,我們將學習使用Golang編寫計算單利的程式。這是一種在銀行和金融領域根據本金、年利率和時間等因素計算貸款利息的方法。

公式

simple_Interest = ( p * r * t) / 100
P = Principal amount
R = Rate per annum
T = Time

例如,假設本金為1000,利率為4,期限為2年,則單利為:

Simple_interest = (1000 * 4 * 2) / 100
= 80

在函式中計算單利

演算法

  • 步驟1 - 宣告用於儲存本金、利率、時間和單利的變數,資料型別為float64。

  • 步驟2 - 從使用者處獲取本金、利率和時間輸入。

  • 步驟3 - 使用上述公式在函式中計算單利。

  • 步驟4 - 列印結果。

示例1

在這個示例中,我們將在函式中計算單利。

時間複雜度

O(1) - 時間複雜度為常數,因為無論輸入如何,程式執行時間都相同。

空間複雜度

O(1) - 程式中的變數是靜態的,因此空間複雜度也是常數。

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, simpleInterest float64 fmt.Println("Program to find simple interest.") // initializing the principal principal = 3000 // initializing the rate rateOfInterest = 4 // initializing the time = 3 // finding the simple interest simpleInterest = (principal * rateOfInterest * time) / 100 // printing the result fmt.Println("principal =", principal, "\nRate of Interest =", rateOfInterest, "\nTime =", time) fmt.Println("The simple interest is", simpleInterest) fmt.Println("(Finding the simple interest within the function)") }

輸出

Program to find simple interest.
principal = 3000 
Rate of Interest = 4 
Time = 3
The simple interest is 360
(Finding the simple interest within the function)

程式碼描述

  • var principal, rateOfInterest, time, simpleInterest float64 - 在這一行中,我們聲明瞭稍後將使用的本金、利率、時間和單利。由於利息可能是小數,因此我們使用了float資料型別。

  • simpleInterest = (principal * rateOfInterest * time) / 100 - 在這行程式碼中,我們應用公式並計算單利。

  • fmt.Println("The simple interest is", simpleInterest) - 最後列印結果。

在不同函式中計算單利

演算法

  • 步驟1 - 宣告用於儲存本金、利率、時間和單利的變數,資料型別為float64。

  • 步驟2 - 從使用者處獲取本金、利率和時間輸入。

  • 步驟3 - 使用本金、利率和時間作為引數呼叫函式,並將函式返回的單利儲存起來。

  • 步驟4 - 列印結果。

示例2

在這個示例中,我們將單利計算放在單獨的函式中,並在需要列印結果的函式中呼叫它。

package main // fmt package provides the function to print anything import "fmt" func simpleInterestFunction(principal, rateOfInterest, time float64) float64 { // finding the simple interest simpleInterest := (principal * rateOfInterest * time) / 100 // returning the simple interest return simpleInterest } func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, simpleInterest float64 fmt.Println("Program to find simple interest.") // taking the principal as input from the user fmt.Print("Please enter the value of the principal amount = ") fmt.Scanln(&principal) // taking the rate of interest as input from the user fmt.Print("Please enter the value of the rate of interest = ") fmt.Scanln(&rateOfInterest) // taking the value of the time as input from the user fmt.Print("Please enter the value of the time = ") fmt.Scanln(&time) // calling the simple interest function by passing the respective parameter // and storing the result simpleInterest = simpleInterestFunction(principal, rateOfInterest, time) // printing the result fmt.Println("The simple interest is", simpleInterest) fmt.Println("(Finding the simple interest in different function)") }

輸出

Program to find simple interest.
Please enter the value of the principal amount = 10000
Please enter the value of the rate of interest = 3
Please enter the value of the time = 4
The simple interest is 1200
(Finding the simple interest in different function)

程式碼描述

  • var principal, rateOfInterest, time, simpleInterest float64 - 在這一行中,我們聲明瞭稍後將使用的本金、利率、時間和單利。由於利息可能是小數,因此我們使用了float資料型別。

  • simpleInterest = (principal * rateOfInterest * time) / 100 - 在這行程式碼中,我們應用公式並計算單利。

  • fmt.Println("The simple interest is", simpleInterest) - 最後列印結果。

  • fmt.Scanln(&time) - 從使用者處獲取時間輸入。

  • simpleInterest = simpleInterestFunction(principal, rateOfInterest, time)

  • - 在這行程式碼中,我們透過傳遞相應的引數來呼叫計算單利的函式。

  • fmt.Println("The simple interest is", simpleInterest) - 最後列印結果。

結論

這是在Golang中計算單利的兩種方法。就模組化和程式碼可重用性而言,第二種方法更好,因為我們可以隨時在專案中呼叫該函式。要了解更多關於Go的資訊,您可以瀏覽這些教程

更新於:2022年8月29日

409 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.