AIM:
A program to solve a quadratic equation, using OOP techniques.
ALGORITHM:
1) Start the process
2) Invoke the classes
3) Get the input for a,b,c;
4) Call the function getinfo() and display()
5) Check if a=0
a) True : compute c/b;
i) Print the value of a/c;
b) False: compute b*b-4*a*c;
i) If ( b*b-4*a*c)=0
ii) Call img();
iii) Otherwise : call real(a,b,c);
6) Stop the process
PROGRAM:
#include<iostream.h> #include<conio.h> class equation { private:float a,b,c; public: void getinfo(float a, float b,float c ); void display( ); void equal(float a, float b); void imag( ); void real(float a,float b,float c); }; void equation :: getinfo(float aa,float bb,float cc) { a=aa; b=bb; c=cc; } void equation::display( ) { cout<<endl; cout<<”a=”<<a<<’\t’; cout<<”b=”<<b<<’\t’; cout<<”c=”<<c<<endl; } void equation ::equal (float a,float b) { float x; x = -b/(2*a); cout<<”Roots are equal “<<x<<endl; } void equation :: imag( ) { cout<<”Roots are imaginary”; } void equation :: real(float a,float b,float det) { float x1,x2,temp; temp = sqrt(det); x1= (-b + temp)/(2*a); x2 = (-b –temp)/(2*a); cout<<”Roots are real”; cout<<”x1= “<<x1<<endl; cout<<”x2 =”<<x2<<endl; } void main( ) { class equation e; float aa,bb,cc; clrscr( ); cout<<”Enter the three numbers”; cin>>aa>>bb>>cc; e.getinfo(aa,bb,cc); e.display( ); if(aa = =0) { float temp; temp = cc/bb; cout<<” Linear Roots”<<temp<<endl; } else { float det; det = (bb*bb – 4*aa*cc); if(det = =0) e.equal(aa,bb); else if (det<0 ) e.imag( ); else e.real(aa,bb,cc ); } getch( ); } |
OUTPUT:
Enter the three numbers 2 4 1
Roots are imaginary
X1= – 0.292893
X2= – 1.707107