COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Selections/Decisions MinValue.java PrintingService.java QuadraticEquation.java PayrollWithDecision.java MultiSelection.java CalculatorWithSwitch.java GradeBookWithSwitch.java StringComparison.java



QuadraticEquation Program


Description

The roots of the quadratic equation ax2 + bx + c = 0 are given by the following formula:



The program prompts the user to input the value of a, b, and c, and then calculates and outputs the root(s).

Associated Concepts:

Math.sqrt() method

Two-way selection structure

Nested selection structure

Algorithm:

Input a from the keyboard.

If a is zero, the program should output an error message

Otherwise

    Input b and c from the keyboard.

    Calculate the term b2 – 4ac (discriminant)

    If the discriminant < 0

        Output a message to indicate there is no real root

    Otherwise

        If the discriminant is 0

            Calculate and output one single root

        Otherwise

            Calculate and output two roots

Source Code



Sample Run



Video