Go Program to Find the Average of Three Numbers

In this post, we will learn how to write a program to find the average of three numbers using the Go programming language.

This tutorial asks the user to enter three integers (a, b, and c), then it computes the average of the three numbers using the formula: (a + b + c) / 3.

Since the total number of terms is 3, we will divide the sum by 3 to get the average.

Go Program to Find the Average of Three Numbers

package main

import (
	"fmt"
)

func main() {
	var a, b, c, sum, average float64

	fmt.Print("Enter the first integer: ")
	fmt.Scanln(&a)

	fmt.Print("Enter the second integer: ")
	fmt.Scanln(&b)

	fmt.Print("Enter the third integer: ")
	fmt.Scanln(&c)

	sum = a + b + c
	average = sum / 3

	fmt.Print("The average of three number is = ", average)

}

Output

Enter the first integer: 8
Enter the second integer: 5
Enter the third integer: 14
The average of three number is = 9

How Does This Program Work?

	var a, b, c, sum, average float64

We have declared five float data type variables. The three variables (a, b, and c) will be used to store the integers entered by the user, while the sum and average variables will be used to store the sum and average of three numbers, respectively.

	fmt.Print("Enter the first integer: ")
	fmt.Scanln(&a)

	fmt.Print("Enter the second integer: ")
	fmt.Scanln(&b)

	fmt.Print("Enter the third integer: ")
	fmt.Scanln(&c)

The program prompts the user to enter three integers. The three integers are taken as input using the fmt.Scanln() method.

	sum = a + b + c

The sum of three variables is calculated with the help of the addition (+) operator. The obtained result is saved in the variable sum.

	average = sum / 3

The average of three numbers is then calculated using the formula: average = sum of three numbers / 3.

The average of the three numbers gets stored in the average named variable.

	fmt.Print("The average of three number is = ", average)

In the end, the average of three numbers, which is stored in the average named variable, is printed on the screen using the fmt.Print() method.

Conclusion

In this post, you learned how to write a program to find the average of three numbers using the Go programming language.

If you have any doubts regarding the tutorial, leave your queries in the comment section.

Leave a Comment

Your email address will not be published. Required fields are marked *