PROGRAM 2:
Compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.
ALGORITHM:
STEP 1: START
STEP 2: Read a, b, c
STEP 3: if a=0 and b=0 then print “Roots cannot be computed” goto step 9
STEP 4: if (a==0) then
root1= -c/b
print root1,goto step 9
STEP 5: Calculate the discriminant value, d = b*b-4*a*c
STEP 6: if(d==0) then root1=root2 =-b/(2*a);
print root1 and root2, goto step 9
STEP 7: if(d>0) then
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
print root1 and print root2, goto step 9
STEP 8: if(d<0) then
real=-b/(2*a)
img=sqrt(fabs(d))/(2*a)
root1=real+i(imag)
root2=real-i(imag)
Print root1 and root2,goto step 9
STEP 9: STOP
FLOWCHART-
PROGRAM:
/* Program to Compute the roots of quadraticequation*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,d,real,img,root1,root2;
printf("Enter the coefficients of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
{
printf("Roots can't be computed\n \n");
exit(0);
}
if(a==0)
{
printf("The roots are linear\n");
root1=-c/b;
printf("root=%f\n",root1);
exit(0);
}
d=b*b-4*a*c;
if(d==0)
{
root1=-b/(2*a);
printf("Roots are Real and Equal\n Root1=Root2=%f\n",root1);
}
else if(d>0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("Roots are Real and Distinct\n Root1=%f\n Root2=%f\n",root1,root2);
}
else
{
printf("Roots are Complex/Imaginary\n");
real=-b/(2*a);
img=sqrt(fabs(d))/(2*a);
printf("Root1= %f +i %f\n",real,img);
printf("Root2= %f -i %f\n",real,img);
}
}