Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in c++ The roots of the quadratic equation ax 2 + bx + c = 0, a 0 are given by t

ID: 3850176 • Letter: I

Question

in c++

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

In this formula, the term b2 – 4ac is called the discriminant. If b2 – 4ac = 0, then the equation has a single (repeated) root. If b2 – 4ac > 0, the equation has two real roots. If b2 – 4ac < 0, the equation has two complex roots. Write a program that prompts the user to input the value of a (the coefficient of x2), b (the coefficient of x), andc (the constant term) and outputs the type of roots of the equation. Furthermore, if b2 – 4ac 0, the program should output the roots of the quadratic equation. (Hint: Use the functions pow() and sqrt() from the header file to calculate the square root. Chapter 3 explains how the functions pow() and sqrt() are used.)

Turn in: Source code and output showing test results (screen shot or picture)

File name included as comment at top of source file

IPO chart included as comments following the file name

Variable names are meaningful

Program compiles

Program prompts for input

Program output is formatted neatly

Program produces correct results

Program is thoroughly tested with test output included

Explanation / Answer

#include <iostream>
#include <math.h>
using namespace std;

int main()
{

    float a, b, c, X, Y, discriminant, Real, Imaginary;
    cout << "Enter coefficients a, b and c of quadratic equation ax2 + bx + c = 0: ";
    cin >> a >> b >> c;
    discriminant = b*b - 4*a*c;
  
    if (discriminant > 0)
  
   {
        X = (-b + sqrt(discriminant)) / (2*a);
        Y = (-b - sqrt(discriminant)) / (2*a);
        cout << "Roots are real" << endl;
        cout << "Roots are "<< X << " " << Y <<endl;
      
    }
  
    else if (discriminant == 0)
   {
        cout << "Roots are real and equal" << endl;
        X = (-b + sqrt(discriminant)) / (2*a);
        cout << "Roots are "<< X <<" " << X << endl;
    }

    else
   {
        Real   = -b/(2*a);
        Imaginary =sqrt(-discriminant)/(2*a);
        cout << "Roots are complex " << endl;

        cout <<"Roots are "<< Real   << "+" << Imaginary << "i" <<" "<< Real << "-" << Imaginary << "i" << endl;
    }

    return 0;
}