C++ Program: Roots of a Quadratic Equation

 // 7 C++ program – Roots of a quadratic equation

/* 7

Write a C++ program to create a class with data members a, b, c andmember functions to input data, compute the discriminant based on the

following conditions and print the roots.

 If discriminant = 0, print the roots are equal and their value.

 If discriminant > 0, print the real roots and their values.

 If discriminant < 0, print the roots are imaginary and exit the program.

*/


#include<iostream.h>

#include<conio.h>

#include<math.h>


class Quadratic

{

private:

int a, b, c;

float disc, x, x1, x2;

public:

void readdata( );

void compute( );

void display( );

};


void Quadratic::readdata( )

{

cout<<"Enter the values for a, b, c (Co-efficeient)"<<endl;

cin>>a>>b>>c;

}


void Quadratic::compute( )

{

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

}


void Quadratic::display( )

{

compute( );

if(disc == 0)

{

cout<<"Equal Roots..."<<endl;

x=-b/(2*a);

cout<<"Root is...."<<x;

}

else if(disc>0)

{

cout<<"Real and Distinct Roots..."<<endl;

x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

cout<<"Root 1 is "<<x1<<endl;

cout<<"Root 2 is "<<x2<<endl;

}

else

cout<<"Imaginary Roots..."<<endl;

}


void main( )

{


Quadratic q;

clrscr( );

q.readdata( );

q.display( );

getch( );

}

Comments

Popular posts from this blog

Karnataka I PUC Computer Science 2024 Study Material | SECOND PUC HANDBOOK EXAM 2025

PYTHON: Tips and Tricks