Program 2
Program 2
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
Algorithm
Step-1: [Read values of A, B, C]-Read A, B, C.
Step-2: [Check for Co-efficient]-If A=B=C=0 then roots are non-determinant
Step-3:[Compute the value Disc]- Disc=B*B-4*A*C;
Step-4:[Different roots are obtained depending on the value of disc]-
If Disc is lesser than 0 than goto step 5
If Disc is equals to 0 than goto step 6
If Disc is greater than 0 than goto step 7.
Step-5: Print Imaginary roots.
Realp=-B/2*A;
Imagp=sqrt(Disc)/2*A;
Root1=realp + imagep;
Root2 = realp – imagep;
Step-6:Roots are real and equal
Root1=B/2*A;
Root2 = root1;
Step-7: roots are real and distinct
Root1= – B +sqrt(Disc)/2*A;
Root2 = -B -sqrt(Disc)/2*A;
Step-8: Stop
Flow Chart
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float a,b,c,disc;
float root1,root2,realp,imagp;
printf("Enter values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("roots cannot be determined\n");
exit(0);
}
disc=b*b-4*a*c;
if(disc==0)
{
printf("roots are real and equal\n");
root1=-b/(2*a);
root2=root1;
printf("root1=%.2f\n",root1);
printf("root2=%.2f\n",root2);
}
else if(disc>0)
{
printf("roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("root1=%.2f\n",root1);
printf("root2=%.2f\n",root2);
}
else
{
realp=-b/(2*a);
imagp=sqrt(fabs(disc))/(2*a);
printf("roots are complex\n");
printf("root1=%.2f+i%.2f\n",realp,imagp);
printf("root2=%.2f-i%.2f\n",realp,imagp);
}
}