Golang Program to find square root of a number


March 4, 2022, Learn eTutorial
1347

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What is the square root of a number?

Square root of a number is the number that we multiply by itself to get the original number.

How to find the square root of a number

Most of the programming languages have predefined mathematical functions to find square root for given numbers.

How to find the square root of a number in the GO Program

Here we are showing to find the square root of a number in the Go language. Here variables num for holding the number for the square root check, and other variable sqrt, for holding the result. The square root of the number is found out by using math.Sqrt(num). Here we must include the header file math for using this built-in function. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, math

STEP 2: Start function main()

STEP 3: Declare the variable num, sqrt

STEP 4: Read the number num using fmt.Scanfln()

STEP 5:Find the square root of a number num using math.Sqrt(num)

STEP 6: Save the result into the variable sqrt

STEP 7: Print the result using fmt.Println()

 

Golang Source Code

                                          package main

import (
    "fmt"
    "math"
)

func main() {

    var n, sqrt float64

    fmt.Print("\nEnter the number to find Square root = ")
    fmt.Scanln(&n)

    sqrt = math.Sqrt(n)

    fmt.Println("\nThe Square root of a number = ", sqrt)
}
                                      

OUTPUT

Enter the number to find Square root = 81

The Square root of a number  = 9