如何使用 Golang 程式碼計算複利?
在本教程中,我們將瞭解如何在 Golang 中編寫計算複利的程式。它是一種在銀行和金融領域使用本金、年利率和時間等因素來計算貸款準確利息的方法。由於準確性更高,利率也比單利高。
公式
compound_Interest = P * ( 1 + R / 100) ^ T P = Principal amount R = Rate per annum T = Time
例如,假設本金為 1000,利率為 4,期限為 2 年,則複利為。
compound_interest = 1000 * (1 + 4 / 100 ) ^2 = 1081.6000000000001
在函式中查詢複利
演算法
步驟 1 - 宣告用於儲存本金、利率、時間和複利的變數,資料型別為 float64。
步驟 2 - 從使用者獲取本金、利率和時間輸入。
步驟 3 - 使用上述公式在函式中計算複利。
步驟 4 - 列印結果。
時間複雜度
O(1) - 時間複雜度為常數,因為無論輸入是什麼,程式都會花費相同的時間。
空間複雜度
O(1) - 程式中的變數是靜態的,因此空間複雜度也是常數。
示例 1
在這個示例中,我們將找到函式內的複利。
package main // fmt package provides the function to print anything import ( "fmt" "math" ) func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, compoundInterest float64 fmt.Println("Program to find compound interest.") // initializing the principal principal = 3000 // initializing the rate rateOfInterest = 4 // initializing the time = 3 // finding the compound interest compoundInterest = (principal * math.Pow(1+rateOfInterest/100, time)) // printing the result fmt.Println("Principal =", principal, "\nRate of Interest", rateOfInterest, "\nTime", time, "\nThe compound interest=", compoundInterest) fmt.Println("(Finding the compound interest within the function)") }
輸出
Program to find compound interest. Principal = 3000 Rate of Interest 4 Time 3 The compound interest= 3374.592 (Finding the compound interest within the function)
程式碼描述
var principal, rateOfInterest, time, compoundInterest float64 - 在這行程式碼中,我們聲明瞭稍後將使用的本金、利率、時間和複利。由於利息可能是小數,因此我們使用了浮點資料型別。
fmt.Scanln(&principal) - 從使用者獲取本金輸入。
fmt.Scanln(&rateOfInterest) - 從使用者獲取利率輸入。
fmt.Scanln(&time) - 從使用者獲取時間輸入。
compoundInterest = (principal * math.Pow(1+rateOfInterest/100, time)) − 在這行程式碼中,我們應用了公式並計算了複利。此外,為了找到 1+rateOfInterest/100 的時間次冪,我們使用了 Golang 中的數學庫,該庫具有 Pow() 函式,該函式有兩個浮點型別的引數。
fmt.Println("The compound interest is", compoundInterest) - 最後列印結果。
在不同的函式中查詢複利
演算法
步驟 1 - 宣告用於儲存本金、利率、時間和複利的變數,資料型別為 float64。
步驟 2 - 從使用者獲取本金、利率和時間輸入。
步驟 3 - 使用本金、利率和時間作為引數呼叫函式,並將函式返回的複利儲存起來。
步驟 4 - 列印結果。
示例 2
在這個示例中,我們將分別在函式中查詢複利,並在需要列印的函式中呼叫它。
package main // fmt package provides the function to print anything import ( "fmt" "math" ) func compoundInterestFunction(principal, rateOfInterest, time float64) float64 { // finding the compound interest compoundInterest := (principal * math.Pow(1+rateOfInterest/100, time)) // returning the compound interest return compoundInterest } func main() { // declaring the floating variables using the var keyword // for storing the principal, rate of interest, and time var principal, rateOfInterest, time, compoundInterest float64 fmt.Println("Program to find compound 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 compound interest function by passing the respective parameter // and storing the result compoundInterest = compoundInterestFunction(principal, rateOfInterest, time) // printing the result fmt.Println("The compound interest is", compoundInterest) fmt.Println("(Finding the compound interest in different function)") }
輸出
Program to find compound interest. Please enter the value of the principal amount = 1000 Please enter the value of the rate of interest = 5 Please enter the value of the time = 3 The compound interest is 1157.6250000000002 (Finding the compound interest in different function)
程式碼描述
var principal, rateOfInterest, time, compoundInterest float64 - 在這行程式碼中,我們聲明瞭稍後將使用的本金、利率、時間和複利。由於利息可能是小數,因此我們使用了浮點資料型別。
fmt.Scanln(&principal) - 從使用者獲取本金輸入。
fmt.Scanln(&rateOfInterest) - 從使用者獲取利率輸入。
fmt.Scanln(&time) - 從使用者獲取時間輸入。
compoundInterest = compoundInterestFunction(principal, rateOfInterest, time)
− 在這行程式碼中,我們透過傳遞相應的引數來呼叫計算複利的函式。此外,為了找到 1+rateOfInterest/100 的時間次冪,我們使用了 Golang 中的數學庫,該庫具有 Pow() 函式,該函式有兩個浮點型別的引數。
fmt.Println("The compound interest is", compoundInterest) - 最後列印結果。
結論
以上是兩種在 Golang 中查詢複利的方法。從模組化和程式碼可重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 Go 的知識,您可以瀏覽這些 教程。