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 fmt package is imported for input and output operations, while the math package is used to calculate the square root for the discriminant.
  • Function Definition: The solveQuadratic function 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 main function prompts the user to enter the coefficients (a, b, c) of the quadratic equation and then calls the solveQuadratic function 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

    1. Install Go: First, ensure that Go is installed on your system. You can download it from https://golang.org/dl/.
    2. Create a New Go File: Save the provided code in a new file with a .go extension, for example, quadratic.go.
    3. 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
  1. 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.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)