C++ Program Code For finding the roots of a quadratic equation

Leave a Comment
/* The following C++ Code will calculate the roots of the quadratic equation, user is prompted to enter the coefficient a, b, & c. The result is displayed after that. */  


#include<iostream.h>
#include<stdlib.h>
#include<math.h>

void main()
{
 float a,b,c,d,r1,r2;
 cout<<"\nFor ax^2+bx+c\n";
 cout<<"Enter value of coefficient a, b & c : ";
 cin>>a>>b>>c;
 d=pow(b,2)-4*a*c;
 if(d==0)
 {
  r2=r1=(-b)/(2*a);
  cout<<"Roots are real & equal";
 }
 else if(d>0)
 {
  r1=-(b+sqrt(d))/(2*a);
  r2=-(b-sqrt(d))/(2*a);
  cout<<"Roots are Real & Distinct";
 }
 else //for case of d<0
 {
  cout<<"Roots are imaginary";
  system("pause");
  exit(0);
 }
 cout<<"\nRoot 1= "<<r1<<"\nRoot 2= "<<r2;
 system("pause");
}


 Screenshots:
C++ Program Code For finding the roots of a quadratic equation

0 comments: