(Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following formula:
b2 − 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for a, b and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots.”
Note you can use Math. pow(x, 0.5) to compute
Algebra: solve quadratic equation
Program Plan:
- Import the required packages.
- Create a class Exercise
- Define the main method.
- Define the scanner input and prompt user to enter the value of a,b,c.
- Get the input.
- Calculate the discriminant value.
- Condition to validate the discriminant value.
- After validation the value gets of the root gets calculated based on the condition.
- Display the result of roots.
The below program is used to solve the quadratic equation:
Explanation of Solution
Program:
//import the required packages
import java.util.Scanner;
//define the class exercise
public class Exercise
{
public static void main(String[] args)
{
//scanner input
Scanner input = new Scanner(System.in);
/*prompt user to enter the value of the a,b and c*/
System.out.print("Enter a, b, c: ");
//get value of a
double a = input.nextDouble();
//get value of b
double b = input.nextDouble();
//get value of c
double c = input.nextDouble();
//equation to calculate the discriminant
double discriminant = b * b - 4 * a * c;
//condition to validate the discriminant value
if (discriminant < 0)
{
//display result
System.out.println("The equation has no real roots");
}
//condition to validate the discriminant value
else if (discriminant == 0)
{
//equation to calculate the root value
double r1 = -b / (2 * a);
//display result
System.out.println("The equation has one root " + r1);
}
//condition to validate the discriminant value
else
{
// (discriminant > 0)
//equation to calculate the root value
double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
//equation to calculate the root value
double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
//display result
System.out.println("The equation has two roots " + r1 + " and " + r2);
}
}
}
Enter a, b, c: 1
2.0
1
The equation has one root -1.0
Additional Output 1:
Enter a, b, c: 1.0
3
1
The equation has two roots -0.3819660112501051 and -2.618033988749895
Additional Output 2:
Enter a, b, c: 1
2
3
The equation has no real roots
Want to see more full solutions like this?
Chapter 3 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
Management Information Systems: Managing The Digital Firm (16th Edition)
SURVEY OF OPERATING SYSTEMS
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Concepts Of Programming Languages
Problem Solving with C++ (10th Edition)
- (Geometry: great circle distance) The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographi- cal latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula: d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2)) Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average earth radius is 6,378.1 km. The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees.arrow_forward(proof by contraposition) If the product of two integers is not divisible by an integer n, then neither integer is divisible by narrow_forward(Algebra: solve linear equations) Write a function that solves the following 2 x 2 system of linear equation: aoox + a01y = bo boa11 - bjao1 X = bja00 - boa10 a10x + any = bị y = agoa11 dooa11 - d01a10 The function header is const int SIZE = 2; bool linearEquation(const double a[][SIZE], const double b[], double result[]); The function returns false if apo1 - aoja10 is 0; otherwise, returns true. Write a test program that prompts the user to enter ao0. d01, a10, a11, bo, bị, and display the result. If aooa11 – a0ia10 is 0, report that "The equation has no solution". A sample run is similar to Programming Exercise 3.3.arrow_forward
- [Fish Tank] You play with a clown fish that has an initial size so. The fish can eat other fish in a tank organized in m columns and n rows. The fish at column i and row j has a positive size si,j. When your fish eats another fish, it grows by that amount. For example, if your clown fish has a size of 10 and eats a fish of size 5, it becomes of size 15. You cannot eat a fish that is bigger than your size. The game starts by eating any fish in the first (left-most) column that is not bigger than yours. After that, you advance one column at a time by moving right. You have only three allowed moves. You either stay at the same row, move one row higher or one row lower. You will always move to the right. Thus, you will make exactly m moves to advance from left to right. Your goal is to exit the fish tank from the right with the biggest possible size. The figure below shows an example with the best answer highlighted. In this case, the final fish size is 71 (10+8+7+24+22). You are required…arrow_forward(Perfect Numbers) An integer number is said to be a perfect number if its factors, including1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 =1 + 2 + 3. Write a function isPerfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.Challenge the power of your computer by testing numbers much larger than 1000.arrow_forward(Python matplotlib or seaborn) CPU Usage We have the hourly average CPU usage for a worker's computer over the course of a week. Each row of data represents a day of the week starting with Monday. Each column of data is an hour in the day starting with 0 being midnight. Create a chart that shows the CPU usage over the week. You should be able to answer the following questions using the chart: When does the worker typically take lunch? Did the worker do work on the weekend? On which weekday did the worker start working on their computer at the latest hour? cpu_usage = [ [2, 2, 4, 2, 4, 1, 1, 4, 4, 12, 22, 23, 45, 9, 33, 56, 23, 40, 21, 6, 6, 2, 2, 3], # Monday [1, 2, 3, 2, 3, 2, 3, 2, 7, 22, 45, 44, 33, 9, 23, 19, 33, 56, 12, 2, 3, 1, 2, 2], # Tuesday [2, 3, 1, 2, 4, 4, 2, 2, 1, 2, 5, 31, 54, 7, 6, 34, 68, 34, 49, 6, 6, 2, 2, 3], # Wednesday [1, 2, 3, 2, 4, 1, 2, 4, 1, 17, 24, 18, 41, 3, 44, 42, 12, 36, 41, 2, 2, 4, 2, 4], # Thursday [4, 1, 2, 2, 3, 2, 5, 1, 2, 12, 33, 27, 43, 8,…arrow_forward
- (?, ?, ?) = ?? + (???) Use a table to express the values of each of these Boolean functions.arrow_forwardHeat capacity of a solid: Debye's theory of solids gives the heat capacity of a solid at temperature T to be 3 T rOp/T Cy = 9VpkB (e* – 1)2 dx, - where V is the volume of the solid, p is the number density of atoms, kg is Boltzmann's constant, and 0D is the so-called Debye temperature, a property of solids that depends on their density and speed of sound. Develop a computer code to evaluate Cy (T) for a given value of the temperature, for a sample consisting of 1000 cubic centimeters of solid aluminum, which has a number density of p = 6.022 x 1028m-3 and a Debye temperature of 0p = 428K. The Boltzmann's constant kg = 1.380649 x 10-23 J · K-1. Please evaluate the integral with the following methods: (a) MATLAB adaptive Simpson quadrature, [Q.FCNT] = QUAD(FUN,A,B,TOL) with TOL =le-10.arrow_forward(x² If h(x) X 2 , then 2arrow_forward
- Please write a C++ coding with modularity using functions. 15. (Numerical) a. Euclid’s method for finding the greatest common divisor (GCD) of two positive integers consists of the following steps:Step 1: Divide the larger number by the smaller and retain the remainder.Step 2: Divide the smaller number by the remainder, again retaining the remainder.Step 3: Continue dividing the previous remainder by the current remainder until the remainder is zero, at which point the last non-zero remainder is the GCD. For example, if the two positive integers are 84 and 49, you have the following: Step 1: 84/49 yields a remainder of 35.Step 2: 49/35 yields a remainder of 14.Step 3: 35/14 yields a remainder of 7.Step 3: 14/7 yields a remainder of 0. Therefore, the last non-zero remainder, which is 7, is the GCD of 84 and 49.Using Euclid’s algorithm, replace the stub function written for Exercise 14 with an actual function that determines and returns the GCD of its two integer arguments. thank you…arrow_forward(GREATEST COMMON DIVISOR) The greatest common divisor of integers x and y is the largest integer that evenly divides into both x and y. Write and test a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd (x, y) is x; otherwise, gcd (x, y) is gcd (y, x % y), where % is the remainder operator.arrow_forwardTrigonometry: The basic MATLAB trigonometric functions are sin, cos, tan, cot, sec, and csc. The inverses, e.g., arcsin, arctan, etc., are cal- culated with asin, atan, etc. The same is true for hyperbolic functions. The inverse function at an2 takes two arguments, y and x, and gives the four- quadrant inverse tangent. The argument of these functions must be in radians. Calculate the following quantities: sin, cost, and tan. sin²+ cos². (Typing sin^2(x) for sin²x will produce an error). y cosh²z-sinh² x, with x = 32m.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning