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(principle, rateOfInterest, time)
-
- 在这行代码中,我们通过传递各自的参数来调用寻找简单利息的函数。
-
fmt.Println(“The simple interest is”, simpleInterest) – 在最后打印结果。
总结
这就是在Golang中寻找单利的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用该函数。