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

Popular posts from this blog

Arrays in C Language

Inline V/S Block Level Element

Variable Naming & Scope of Variable

Variables in Go lang

Identifiers

String in golang

Identify Variable type and values in go lang

Decision Making in Go lang

Installing go lang

Constant in Go lang