Brijmohan lal Sahu - Facebook
ads
ads

Monday, September 30, 2019

Program to Solve Quadratic Equation Using if else and Switch

/* Program to Solve Quadratic Equation Using if else and Switch */
/* In quadratic equation we need values of a,b,c
and D=(b square -4.a.c)
and every thing depends on the value of D
*If D grater than 0 multiple and real root
*If D==0 only single root
*If D is less than 0 than imaginary roots
*/

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int a,b,c,D,n;
float x1,x2,D1;
clrscr();

cout<<"\nEnter Values";
cout<<"\n a = ";
cin>>a;
cout<<"\n b = ";
cin>>b;
cout<<"\n c = ";
cin>>c;

cout<<"\nEnterd Values are : a="<<a<<"b="<<b<<"c"<<c;
cout<<"\nQuadratic Equation Calculation:";

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

cout<<"\nValue of Normal D= "<<D;
D1=abs(D);
D1=sqrt(D1);

cout<<"\nValue of Squared D="<<D1;

if(D>=0)
{
//----------------- Check for exactly D==0
if(D==0)
{ n=2;}
else { n=1;} 
}
else
{ n=3;}

cout<<"\nValue of n :"<<n;

switch(n)
{
case 1:
//Multiple Roots
cout<<"\nReal & Multiple Roots";
x1=((b+D1)/(2*a));
x2=((b-D1)/(2*a));
cout<<"\n\nRoots are X1="<<x1<<" X2= "<<x2;
break;

case 2:
                //Single Root
cout<<"\nReal & Single Roots";
x1=((b)/(2*a));
cout<<"\n\nRoots are X1="<<x1;
break;


case 3:
               //Imaginary Part
cout<<"\nImaginary Roots";
cout<<"\nX1=("<<b<<"+"<<D1<<"i)/2*"<<a;
cout<<"\nX1=("<<b<<"-"<<D1<<"i)/2*"<<a;
break;

default:
cout<<"\nWrong Entry, Error!!!";
}

getch();
return 0;
}