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

Program to calculate telephone bill

Arrays in C Language

String in golang

Sum of two numbers in go lang

Pointers in C Language

Decision Making Statements

Say Hello world in Go lang

Decision Making in Go lang

Program to convert time from seconds to hours, minutes and seconds

Installing go lang