How to write a Golang program to find the square root of a number is a common Go programming exercise. Go program for square root is also a popular question during college semester exams and various programming tests. If you are familiar with Go API then writing a Go program that can find the square root of a number using math library is not difficult. You just need to pass a double value and it returns a double which is the square root of the number you passed.
In the next section, we will see the complete code example of finding the square root of a number from the Go program. Another Go coding questions which is very popular and related to programming exercise is writing Go program to find prime numbers, if you are going to appear in Go interviews than its worth looking.
Before discussing the square root code in GO programming language, let’s understand the term square root first.
Square Root
The square of a number is that number times itself. In other terms, when we multiply an integer by itself we call the product the square of the number. Mathematically, the square of a number is given as,
Square of n = n*n
For example, the square of number 4 is 4*4 = 16
The square root is just the opposite of the square. The square root of a number, n, is the number that gives n when multiplied by itself. Mathematically, the square root of a number is given as,
Square Root of n = √n
Now that you know what square and square root of a number are, let’s see different ways to calculate them in Golang.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 | package main import "fmt" import "math" func main() { var x float64 x = math.Sqrt(4) fmt.Println(x) } |
Output:
1 2 3 | 2 |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package main import "fmt" import "math" func main() { var x,result float64 fmt.Print("Enter a number : ") fmt.Scan(&x) result = math.Sqrt(x) fmt.Println(result) } |
Output:
1 2 3 4 | Enter a number : 16 4 |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // Golang program to illustrate how to find // the square root of the given number package main import ( "fmt" "math" ) // Main function func main() { // Finding square root // of the given number // Using Sqrt() function // Displaying the result fmt.Printf("Result 1: %.1f", math.Sqrt(0)) fmt.Printf("\nResult 2: %.1f", math.Sqrt(-100)) fmt.Printf("\nResult 3: %.1f", math.Sqrt(math.Inf(1))) fmt.Printf("\nResult 4: %.1f", math.Sqrt(math.NaN())) fmt.Printf("\nResult 5: %.1f", math.Sqrt(36) ) } |
Output:
1 2 3 4 5 6 7 | Result 1: 0.0 Result 2: NaN Result 3: +Inf Result 4: NaN Result 5: 6.0 |
Find Squre of a Root by User Defined Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package main import "fmt" func main() { var result float64; result=find_square(16) fmt.Println(result) } func find_square(number float64) float64{ var sr float64 = number / 2 var temp float64; for{ temp = sr sr = (temp + (number / temp)) / 2 if(temp - sr) == 0{ break; } } return sr } |