How to Find all Roots of a Quadratic Equation in Golang?


In this tutorial, we will find the root of a quadratic equation using the Golang programming language. The tutorial includes two different ways of programming to find out the roots of the quadratic equation.

Explanation

The equation with power two is called a quadratic equation. The standard equation of the quadratic equation is −

$$\mathrm{a\:x\:^{\wedge}\:2\:+\:b\:x\:+\:c\:=\:0}$$

In the above equation a, b, and c are coefficients where a cannot be zero. To find the nature of the roots of the quadratic equation i.e they are real or imaginary first we need to find the discriminant of the equation using the below formulae

$$\mathrm{D\:=\:b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c}$$

If D is greater than zero then the roots are distinct and real.

If D is equal to zero then the roots are equal and real.

If D is less than zero then the roots are imaginary.

Now we will find the root of the equation using the below formulae −

$$\mathrm{X1\:=\:(-\:b\:+\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

$$\mathrm{X1\:=\:(-\:b\:-\:underRoot\:(b\:^{*}\:b\:-\:4\:^{*}\:a\:^{*}\:c)\:/\:2\:^{*}\:a}$$

Algorithm

Step 1 - Declaring the variables for the coefficients and discriminant.

Step 2 - Initializing the variables with the respective value.

Step 3 - Find the discriminant with the respective formulae.

Step 4 - Find roots according to the value of discriminant.

Example

In this example, we are going to find the roots of the quadratic equation within the function.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
 
   // math package is to use all the math-related predefined functions
   "math"
)
func main() {
   
   // declaring the variables to store the value
   
   // of the coefficients of quadratic equation
   var a, b, c float64
   
   // declaring a variable of float type to store discriminant
   
   // of the quadratic equation
   var d float64
   
   // initializing the coefficients variable with respective value
   a = 1
   b = 1
   c = 1
   fmt.Println("Finding the roots of the equation with the below coefficients within the function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)
   
   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i", imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}

Output

Finding the roots of the equation with the below coefficients within the function:
a = 1
b = 1
c = 1
Roots are complex.
The roots are:
First Root -0.5 + i 0.8660254037844386
Second Root -0.5 - i 0.8660254037844386

Algorithm

Step 1 - Declaring the variables for the coefficients and discriminant.

Step 2 - Initializing the variables with the respective value.

Step 3 - Find the discriminant with the respective formulae.

Step 4 - Find roots according to the value of discriminant.

Example

In this example, we are going to find the roots of the quadratic equation in the separate function.

package main
import (
   
   // fmt package provides the function to print anything
   "fmt"
   
   // math package is to use all the math related predefined functions
   "math"
)
func rootsOfQuadraticEquation(a, b, c float64) {
   
   // declaring a variable of float type to store discriminant
   
   // of the quadratic equation
   var d float64
   
   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i",   imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}
func main() {
   
   // declaring the variables to store the value
   
   // of the coefficients of quadratic equation
   var a, b, c float64
   
   // initializing the coefficients variable with respective value
   a = 4
   b = 12
   c = 9
   fmt.Println("Finding the roots of the equation with the below coefficients in the seperate function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)
   rootsOfQuadraticEquation(a, b, c)
}

Output

Finding the roots of the equation with the below coefficients in the seperate function:
a = 4
b = 12
c = 9Roots are equal and same.
The root is -1.5

Conclusion

These are the two ways to find the root of a quadratic equation in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about Golang you can explore these tutorials.

Updated on: 29-Nov-2022

650 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements