The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 3 Exercise 3.1.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> * Exercise 3.1 - Algebra: Solve Quadratic Equations<br /> */<br />import java.util.Scanner;<br /><br />public class CheckerboardPatternOfAsterisks {<br /> public static void main (String [] args) {<br /> <br /> // declare variables<br /> double a, b, c, disc, r1, r2;<br /> <br /> // create Scanner to read user input<br /> Scanner input = new Scanner(System.in);<br /><br /> // prompt user to enter details<br /> System.out.print("Enter a, b, c: ");<br /> a = input.nextDouble();<br /> b = input.nextDouble();<br /> c = input.nextDouble();<br /><br /> //calculate the discriminant<br /> disc = b * b - (4 * a * c);<br /> <br /> //calculate the roots<br /> r1 = (-b + Math.pow(disc, 0.5)) / (2 * a);<br /> r2 = (-b - Math.pow(disc, 0.5)) / (2 * a);<br /><br /> // display the results<br /> if (disc > 0){<br /> System.out.println("The roots are " + r1 + " and " + r2 + "n");<br /> }<br /> <br /> if (disc == 0){<br /> System.out.println("The root is " + r1 + "n");<br /> }<br /> <br /> if (disc < 0){<br /> System.out.println("The equation has no real roots.n");<br /> }<br /> }<br />}<br /> |
Sample Program run |
Click here to see other solutions to Introduction to Java Programming.