Solving Quadratic Equations in Java

 

 

Introduction

A quadratic equation is a second-degree polynomial equation in the form:

ax2 + bx + c = 0

Where ‘a’, ‘b’, and ‘c’ are constants and ‘x’ represents the unknown variable. The quadratic formula, used to find the values of ‘x’, is given by:

x = (-b ± √(b2 – 4ac)) / 2a

The purpose of this program is to implement the solution of a quadratic equation using the above formula. It will take the coefficients ‘a’, ‘b’, and ‘c’ as inputs and compute the roots of the equation.

Objective

The objective of this Java program is to compute and display the roots of a quadratic equation by using the quadratic formula. It will handle cases where the equation has real and distinct roots, real and equal roots, or complex roots.

Java Code for Solving Quadratic Equations

        import java.util.Scanner;
        
        public class QuadraticSolver {
        
            public static void main(String[] args) {
                // Create a scanner object to get input from the user
                Scanner scanner = new Scanner(System.in);
                
                // Input coefficients a, b, and c
                System.out.print("Enter coefficient a: ");
                double a = scanner.nextDouble();
                System.out.print("Enter coefficient b: ");
                double b = scanner.nextDouble();
                System.out.print("Enter coefficient c: ");
                double c = scanner.nextDouble();
                
                // Calculate the discriminant
                double discriminant = b * b - 4 * a * c;
                
                // Check if the discriminant is positive, zero, or negative
                if (discriminant > 0) {
                    // Two real and distinct roots
                    double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
                    double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
                    System.out.println("The roots are real and distinct.");
                    System.out.println("Root 1 = " + root1);
                    System.out.println("Root 2 = " + root2);
                } else if (discriminant == 0) {
                    // One real and repeated root
                    double root = -b / (2 * a);
                    System.out.println("The roots are real and equal.");
                    System.out.println("Root = " + root);
                } else {
                    // Complex roots
                    double realPart = -b / (2 * a);
                    double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
                    System.out.println("The roots are complex.");
                    System.out.println("Root 1 = " + realPart + " + " + imaginaryPart + "i");
                    System.out.println("Root 2 = " + realPart + " - " + imaginaryPart + "i");
                }
                
                // Close the scanner to avoid resource leak
                scanner.close();
            }
        }

Explanation of the Program Structure

The Java program is structured as follows:

  • Scanner class: Used to read user input from the console.
  • Input coefficients: The program prompts the user to input the values of ‘a’, ‘b’, and ‘c’. These values are then stored in corresponding variables.
  • Discriminant calculation: The program calculates the discriminant (b2 – 4ac) to determine the nature of the roots.
  • Conditional statements: Based on the value of the discriminant, the program determines if the roots are real and distinct, real and equal, or complex. It then calculates the roots accordingly.
  • Math.sqrt(): This method is used to calculate the square root of the discriminant and to handle complex numbers when the discriminant is negative.

How to Run the Program

Follow these steps to run the program:

  1. Save the code: Save the Java code in a file named QuadraticSolver.java.
  2. Compile the code: Open a terminal or command prompt and navigate to the directory where the file is saved. Run the command javac QuadraticSolver.java to compile the code.
  3. Run the program: After compiling, run the program by executing the command java QuadraticSolver.
  4. Input coefficients: The program will prompt you to enter values for ‘a’, ‘b’, and ‘c’. Enter the coefficients as requested.
  5. View the output: The program will display the roots of the quadratic equation based on the entered coefficients.
© 2024 Learn Programming. All Rights Reserved.

 

Leave a Reply

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