Data Structures

Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages

ALGORITHM
Purpose : To find roots of a given quadratic equation.
Input: Coefficients of quadratic equation a, b, c
Output: Root1 and Root2
START
STEP 1: [Input the values of a, b, c]
read a, b, c
STEP 2: [Calculate the determinant]
determinant = b*b-4*a*c
STEP 3: [Check for validity]
If a is equal to 0 and b is equal to 0
print “Invalid Inputs”
STEP 4: [Check for different roots]
If a is equal to 0
print “Linear equation”
Root1=-c/b
Print “Root1”
STEP 5: [Check for real and equal roots]
If determinant is equal to 0
print “Roots are real and equal”
Root1= -b/(2*a)
Root2 = -b/(2*a)
Print “Root1 & Root2”
STEP 6: [Check for real and distinct roots]
If determinant is greater than 0
Then print “Roots are real and distinct”
Root1= (-b+ (sqrt (fabs (determinant))))/(2*a)
Root2= (-b-(sqrt (fabs (determinant))))/(2*a)
Print “root1 and root2”
End if
STEP 7: [Check for imaginary roots]
print “Roots are imaginary”
Real=-b/ (2*a)
Imaginary=sqrt (fabs (determinant))/ (2*a)
print “Root1 and R oot2”
STEP 8: [Finished]
STOP

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c;
float root1,root2;
float determinant,real,imaginary;
clrscr();
printf("Enter the Co-efficient of Quadratic Equation\n");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if(a==0 && b==0)
{
printf("INVALID INPUTS\n");
getch();
}
else if(a==0)
{
printf("LINEAR EQUATION\n");
root1=-c/b;
printf("ROOT=%f\n",root1);
}
else if(determinant==0)
{
printf("ROOTS ARE REAL AND EQUAL\n");
root1=-b/(2*a);
root2=-b/(2*a);
printf("Root1=%f\n Root2=%f",root1,root2);
}
else if(determinant>0)
{
printf("ROOTS ARE REAL AND DISTINCT\n");
root1=(-b+(sqrt(fabs(determinant))))/(2*a);
root2=(-b-(sqrt(fabs(determinant))))/(2*a);
printf("ROOT1=%f\n ROOT2=%f",root1,root2);
}
else
{
printf("ROOTS ARE IMAGINARY\n");
real=-b/(2*a);
imaginary=sqrt(fabs(determinant))/(2*a);
printf("ROOT1=%f+i%f\n",real,imaginary);
printf("ROOT2=%f-i%f\n",real,imaginary);
getch();
}
******************************************************************************
Output 1:
Enter the Co-efficient of Quadratic Equation
000
Invalid Coefficients
Output 2:
Enter the Co-efficient of Quadratic Equation
0 21
Root1=-0.5
Output 3:
Enter the Co-efficient of Quadratic Equation
121
Roots are Real and Equal
Root1=-1.0000
Root2=-1.0000
Output 4:
Enter the Co-efficient of Quadratic Equation
189
Roots are Real and Distinct
Root1=-1.354
Root2=-6.646
Output 5:
Enter the Co-efficient of Quadratic Equation
123
ROOTS ARE IMAGINARY
ROOT1=-1.000+i1.414
ROOT2=-1.000-i1.414
*****************************************************************

Leave a comment

Your email address will not be published. Required fields are marked *