Develop a C++ program to find all roots of a quadratic equation ax2+bx+c=0.

THEORY/ALGORITHM
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:
The syntax of an if...else statement in C++ is:
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Algorithm:
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D←b*b-4*a*c
Step 4: if D≥0
r1←(-b+√D)/2a
r2←(-b-√D)/2a
Display r1 and r2 as roots.
else if D=0
r1←-b/2a
r2←-b/2a
Display r1 and r2 as roots.
else
Display roots are imaginary
Step 5: Stop

PROCEDURE/PROGRAM
--------------------------
if(d>0)
{
cout<<“Two real and distinct roots”;
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout<<“\nRoots are “<<root1<<” and “<<root2;
}
else
if(d==0)
{
cout<<“\nTwo real and equal roots”;
root1=root2=-b/(2*a);
cout<<“\nRoots are “<<root1<<” and “<<root2;
}
else
cout<<“\nRoots are COMPLEX and IMAGINARY….!!!”;

--------------------------------------

RESULT- 
Sample Input and Output:
Input

Enter coefficients a, b and c:
4
5
1
Output
Two real and distinct roots
Roots are -0.25 and -1


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: