Golang 如何计算复利

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 – 在这一行中,我们声明了本金、利率、时间和复利,我们将在以后使用。由于利息可以是十进制的,所以我们使用了float数据类型。

  • **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 – 在这一行中,我们声明了本金、利率、时间和复利,我们将在后面使用。由于利息可以是十进制的,所以我们使用了float数据类型。

  • **fmt.Scanln( &principal) – **从用户那里获取本金的输入。

  • **fmt.Scanln( &rateOfInterest) – **从用户那里获取利率的输入。

  • **fmt.Scanln( &time) – **从用户那里获取时间的输入。

  • compoundInterest = compoundInterestFunction(principle, rateOfInterest, time )

- 在这行代码中,我们通过传递各自的参数来调用寻找复利的函数。此外,为了找到1+rateOfInterest/100的时间幂,我们使用了Golang的数学库,其中有一个Pow()函数,它有两个浮点数类型的参数。

  • fmt.Println(“The compound interest is”, compoundInterest) – 在以后打印结果。

总结

以上是在Golang中查找复利的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用该函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程