Introduction
A quadratic equation is a second-order polynomial equation of the form:
ax² + bx + c = 0
Where a, b, and c are constants, and x is the variable. The solution to this equation can be found using the quadratic formula:
x = (-b ± √(b² – 4ac)) / 2a
The term b² – 4ac is called the discriminant, and it determines the nature of the roots of the equation. If the discriminant is positive, there are two real roots; if it is zero, there is one real root; if it is negative, the equation has complex (imaginary) roots.
Objective
The objective of this program is to implement a solution to a quadratic equation in the Go programming language, where the program calculates the roots (real or complex) based on the coefficients a, b, and c provided by the user.
Go Code for Solving Quadratic Equations
package main
import (
"fmt"
"math"
)
// Function to calculate the roots of the quadratic equation
func solveQuadratic(a, b, c float64) {
// Calculate the discriminant
discriminant := b*b - 4*a*c
// Check if the discriminant is greater than, equal to, or less than zero
if discriminant > 0 {
// Two real roots
root1 := (-b + math.Sqrt(discriminant)) / (2 * a)
root2 := (-b - math.Sqrt(discriminant)) / (2 * a)
fmt.Printf("The roots are real and distinct: x1 = %.2f, x2 = %.2f\n", root1, root2)
} else if discriminant == 0 {
// One real root
root := -b / (2 * a)
fmt.Printf("The root is real and repeated: x = %.2f\n", root)
} else {
// Two complex roots
realPart := -b / (2 * a)
imaginaryPart := math.Sqrt(-discriminant) / (2 * a)
fmt.Printf("The roots are complex: x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart)
}
}
func main() {
// Input coefficients a, b, and c
var a, b, c float64
fmt.Println("Enter the coefficients of the quadratic equation (ax² + bx + c = 0):")
fmt.Print("Enter a: ")
fmt.Scan(&a)
fmt.Print("Enter b: ")
fmt.Scan(&b)
fmt.Print("Enter c: ")
fmt.Scan(&c)
// Solve the quadratic equation
solveQuadratic(a, b, c)
}
Explanation of the Program Structure
The program is structured as follows:
- Importing Packages: The
fmtpackage is imported for input and output operations, while themathpackage is used to calculate the square root for the discriminant. - Function Definition: The
solveQuadraticfunction is responsible for calculating and displaying the roots of the quadratic equation. It uses the quadratic formula and handles three cases based on the discriminant: two real roots, one real root, or complex roots. - Input Handling: The
mainfunction prompts the user to enter the coefficients (a, b, c) of the quadratic equation and then calls thesolveQuadraticfunction to calculate and display the results. - Conditional Logic: The program checks the value of the discriminant to determine whether the roots are real or complex and displays the corresponding results.
How to Run the Program
-
- Install Go: First, ensure that Go is installed on your system. You can download it from https://golang.org/dl/.
- Create a New Go File: Save the provided code in a new file with a .go extension, for example, quadratic.go.
- Run the Program: Open a terminal or command prompt, navigate to the directory where the quadratic.go file is located, and run the following command:
go run quadratic.go
- Enter the Coefficients: The program will prompt you to enter the coefficients a, b, and c. After entering the values, it will display the roots of the quadratic equation.

