Wednesday, October 3, 2018

CPL 3 - ROOTS OF A QUADRATIC EQUATION

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

Algorithm:

Step1:[read the values of A,B and C]
       Read A,B,C

Step2:[check for the co-efficients]
      If  A=0 and B=0 and C=0
       Then roots are non-determinant

Step 3:[compute the value of Disc]
       Disc=B*B-4*A*C;

Step 4:[Depending on the value of disc different roots are obtained]
  If Disc is lesser than 0 go to Step 5
         If Disc is equals to 0 go to step 6
If Disc is greater than 0 go to step 7

Step 5: Print the 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


FLOWCHART:( click on image to zoom )


PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void 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 && b==0 && c==0)
{
printf("roots cannot be determined\n");
exit(0);
}
else
{
disc=b*b-4*a*c;
if(disc>0)
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("roots are real and distinct\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}

else if(disc==0)
{
root1=-b/(2*a);
root2=root1;
printf("roots are real and equal\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}

else if(disc<0)
{
realp=-b/(2*a);
imagp=sqrt(abs(disc)/(2*a));
printf("roots are complex\n");
printf("root1=%f+i%f\n",realp,imagp);
printf("root2=%f-i%f\n",realp,imagp);
}
}

}



OUTPUT:( click on image to zoom )


No comments:

Post a Comment