Question #16428

The roots of the quadratic equation ax2+bx+c=0. In this formula, the term is called discriminant. If , then the equation has a single (repeated) root. If the equation has two real roots. If , the equation has two complex roots ie no real roots. Using the concept of passing values to a function write a C++ program that computes real roots of a quadratic equation.

Expert's answer

#include<iostream>

#include<cmath>

using namespace std;

double a, b, c, D, x1, x2;

void main(){

cout<<"enter the coefficient a: ";

cin>>a;

cout<<"enter the coefficient b: ";

cin>>b;

cout<<"enter the coefficient c: ";

cin>>c;

D = b*b-4*a*c;

if (D==0) cout<<"the single root is "<<-b+sqrt(D)/(2*a)<<"\n";

if (D<0)& cout<<"there are no roots.\n";

if (D>0)& cout<<"The roots are:\n"<<-b+sqrt(D)/(2*a)<<"\n"<<-b-sqrt(D)/(2*a)<<"\n";

system("pause");

}

#include<cmath>

using namespace std;

double a, b, c, D, x1, x2;

void main(){

cout<<"enter the coefficient a: ";

cin>>a;

cout<<"enter the coefficient b: ";

cin>>b;

cout<<"enter the coefficient c: ";

cin>>c;

D = b*b-4*a*c;

if (D==0) cout<<"the single root is "<<-b+sqrt(D)/(2*a)<<"\n";

if (D<0)& cout<<"there are no roots.\n";

if (D>0)& cout<<"The roots are:\n"<<-b+sqrt(D)/(2*a)<<"\n"<<-b-sqrt(D)/(2*a)<<"\n";

system("pause");

}

Learn more about our help with Assignments: C++

## Comments

## Leave a comment