- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- C++ Program to Find All Roots of a Quadratic Equation
- Java Program to Find all Roots of a Quadratic Equation
- Haskell program to find all roots of a quadratic equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- Java program to find the roots of a quadratic equation
- C program to find the Roots of Quadratic equation
- How to write a C program to find the roots of a quadratic equation?
- Finding roots of a quadratic equation – JavaScript
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- Write all the values of k for which the quadratic equation $x^2+kx+16=0$ has equal roots. Find the roots of the equation so obtained.
- Find $p$, if quadratic equation $py( y-2)+6=0$ has equal roots.
- A quadratic equation with integral coefficient has integral roots. Justify your answer.
- Find the roots of the following quadratic equation:$x^{2} -3\sqrt {5}\ x+10=0$
- Find the roots of the quadratic equation $\sqrt{2}x^{2}+7x+5\sqrt{2}=0$.
- Does there exist a quadratic equation whose coefficients are all distinct irrationals but both the roots are rationals? Why?
