# Exercise 3.1 - Algebra: Solve Quadratic Equations

The program below is the answer to Liang's Introduction to Java Programming (9th Edition) Chapter 3 Exercise 3.1. ```/**
*
* @Author: Aghatise Osazuwa
* Website: www.cscprogrammingtutorials.com
* Exercise 3.1 - Algebra: Solve Quadratic Equations
*/
import java.util.Scanner;

public class CheckerboardPatternOfAsterisks {
public static void main (String [] args) {

// declare variables
double a, b, c, disc, r1, r2;

// create Scanner to read user input
Scanner input = new Scanner(System.in);

// prompt user to enter details
System.out.print("Enter a, b, c: ");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();

//calculate the discriminant
disc = b * b - (4 * a * c);

//calculate the roots
r1 = (-b + Math.pow(disc, 0.5)) / (2 * a);
r2 = (-b - Math.pow(disc, 0.5)) / (2 * a);

// display the results
if (disc > 0){
System.out.println("The roots are " + r1 + " and " + r2 + "\n");
}

if (disc == 0){
System.out.println("The root is " + r1 + "\n");
}

if (disc < 0){
System.out.println("The equation has no real roots.\n");
}
}
}
``` Sample Program run
Click here to see other solutions to Introduction to Java Programming.