Home »
Golang »
Golang Programs
Golang - Use of Printf() and Scanf() Functions
Here, we are going to demonstrate the use of Printf() and Scanf() function in Golang (Go Language)?
By Nidhi Last updated : March 28, 2023
Printf() and Scanf() functions in Golang
In this program, we will demonstrate the use of Printf() and Scanf(). In the Go language, we can use Printf() and Scanf() function similar to the C language.
Golang code to demonstrate example of Printf() and Scanf() functions
The source code to demonstrate the use of Printf() and Scanf() function is given below. The given program is compiled and executed successfully.
// Golang program to demonstrate the use of
// printf() and scanf() function.
package main
import "fmt"
func main() {
//Declare 4 integer type variables.
var p int
var r int
var t int
var si int
fmt.Print("Enter principal: ")
fmt.Scanf("%d",&p)
fmt.Print("Enter rate: ")
fmt.Scanf("%d",&r)
fmt.Print("Enter time: ")
fmt.Scanf("%d",&t)
si=(p*r*t)/100
fmt.Println("Simple interest is: ",si)
}
Output
Enter principal: 10000
Enter rate: 3
Enter time: 3
Simple interest is: 900
Explanation
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.
Now, we come to the main() function. The main() function is the entry point for the program. Here, we demonstrate the use of Print() and Scanf() function. Both functions are available in the fmt package and we can use both functions similar to C and C++. Here, we calculated the simple interest and print the result on the console screen.
Golang Basic Programs »