Simple interest in go lang
package main
import "fmt"
func main() {
var p int
var r float32
var t int
fmt.Print("Enter principal, rate and time: ")
fmt.Scan(&p,&r,&t)
si := float32(p) * float32(t) * r / 100
fmt.Printf("Simple interest: %.2f",si)
}
Explanation
- Most of the things of the above program are already discussed. bur some of the important statement is discussed here.
si := float32(p) * float32(t) * r / 100
- The above program statement performs a simple calculation p * t * r / 100 and stores the result in variable si.
- In go lang in a single expression multiple types are not allowed, so we need to convert p and t into float32 type and expression p * t * r becomes a float32 type expression.
- To typecast a variable we use the following syntax
type(var)
- In the above program snippet to print the result fmt.Printf("Simple interest : %.2f",si).
- Printf() is a function used to print the formatted output.
- %.2f prints a floating point value with only 2 decimal places.
Comments