(Algebra: solve quadratic equations) The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 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".

Respuesta :

Answer:

// here is code in c++.

#include <bits/stdc++.h>

using namespace std;

int main()

{

   // variables

  int a,b,c;

  float d;

  cout<<"enter the value of a,b,c (separated by space):";

  // read the value of a,b,c

  cin>>a>>b>>c;

  // calculate discriminant

  d=b*b-(4*a*c);

  // if d<0, then no read root

  if(d<0)

  {

      cout<<"The equation has no real roots"<<endl;

  }

  // d=0, then one real root

  else if(d==0)

  {

      float r=-b/(2*a);

      cout<<"only root of the equation is: "<<r<<endl;

  }

  // d>=0 then two real roots

  else if(d>0)

  {

  // calculate the roots

      float r1=(-b+sqrt(d))/(2*a);

      float r2=(-b-sqrt(d))/(2*a);

      // print the roots

      cout<<"first root of the equation is: "<<r1<<endl;

      cout<<"second root of the equation is: "<<r2<<endl;

  }

return 0;

}

Explanation:

Read the value of a,b,c from user.Calculate discriminant "d" as b^2 - 4ac . If the value of discriminant is less than 0 then there will be no real root. If discriminant is equal to 0 then quadratic equation has only one root which is -b/2a. Calculate the only root and print it.If discriminant is greater than 0 then quadratic equation has two root as r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a). calculate both the root and print them.

Output:

enter the value of a,b,c (separated by space): 2 4 1

first root of the equation is: -0.292893

second root of the equation is: -1.70711