Hands On Program #1

Build a simple Go program that reads two numbers from the user and prints their sum, product, difference, and quotient, reinforcing your understanding of variables, operators, input, and formatted output in Go

Let us write a simple program to accept two numbers and print their sum, product, difference, and quotient. The concepts we have learned so far in our previous posts will be used in this topic.

  1. Go module system
  2. Operators in Go Lang
  3. Variables in Go Lang
  4. Reading Data from console
  5. Writing data to the screen

This will solidify your knowledge and help you move forward. This is a practical post, so you can follow along and run the code on your machine.

Create the project folder

mkdir golab_hands_on_simple_application
cd golab_hands_on_simple_application

[commands may vary based on your OS]

Now initialize the Go module system by executing the go mod init command. This is optional for this simple project because we don’t have many dependencies, but it is a good practice to follow for any real-world application.

go mod init github.com/srnyapathi/golab_hands_on_simple_application

The Go module system will initialize the module.

go mod init github.com/srnyapathi/golab_hands_on_simple_application
go: creating new go.mod: module github.com/srnyapathi/golab_hands_on_simple_application

Writing the program

We will read two numbers from the user, so we need to declare two variables. We also need to prompt the user, and for that we use fmt.Print.

	var num1, num2 float64
	fmt.Print("Enter first number: ")
	fmt.Scan(&num1)
	fmt.Print("Enter second number: ")
	fmt.Scan(&num2)

Logic

You will find variables initialized using the shorthand operator (:=), which handles both variable declaration and assignment.

  1. Sum +
  2. Product *
  3. Difference
  4. Quotient /
    sum := num1 + num2
	product := num1 * num2
	difference := num1 - num2
	quotient := num1 / num2

Output

We use fmt.Printf to print values along with a formatted output.

  1. %.2f\n is a format string
  2. Sum:/Product/Difference/Quotient → literal text that will be printed.
  3. % → starts a placeholder (a “slot” where a value is inserted).
  4. .2 → precision: 2 digits after the decimal point.
  5. f → floating-point number (like float / double).
  6. \n → newline (moves to the next line after printing).
    fmt.Printf("Sum: %.2f\n", sum)
	fmt.Printf("Product: %.2f\n", product)
	fmt.Printf("Difference: %.2f\n", difference)
	fmt.Printf("Quotient: %.2f\n", quotient)

The whole program

package main

import "fmt"

func main() {
	var num1, num2 float64
	fmt.Print("Enter first number: ")
	fmt.Scan(&num1)
	fmt.Print("Enter second number: ")
	fmt.Scan(&num2)

	sum := num1 + num2
	product := num1 * num2
	difference := num1 - num2
	quotient := num1 / num2

	fmt.Printf("Sum: %.2f\n", sum)
	fmt.Printf("Product: %.2f\n", product)
	fmt.Printf("Difference: %.2f\n", difference)
	fmt.Printf("Quotient: %.2f\n", quotient)
}

Running the code

go run main.go

The output will look like this:

Here you go! This reinforces the concepts you have learned so far.

Some things to expand on

  1. Try running this program and supply 0 as one of the inputs (especially the second number) and see what happens. Think about why the output looks the way it does.

Repo Link – https://github.com/srnyapathi/golab_package_examples

srnyapathi
srnyapathi
Articles: 41