Sunday, 29 December 2019

Roots of quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2,img1,img2,disc;
printf("Enter the coefficient 1: ");
scanf("%f",&a);
printf("Enter the coefficient 2: ");
scanf("%f",&b);
printf("Enter the coefficient 3: ");
scanf("%f",&c);
if(a==0.0)
{
if(b==0.0)
printf("Equation is degenarate..");
else
{
printf("Linear equation has single root..\n");
r1=-c/b;
printf("Root is %.2f",r1);
}
}
else
{
disc=(b*b)-(4.0*a*c);
if(disc>0.0)
{
printf("\nReal and distinct roots..");
r1=(-b+sqrt(disc))/(2.0*a);
r2=(-b-sqrt(disc))/(2.0*a);
img1=img2=0.0;
}
else
{
if(disc==0.0)
{
printf("\nReal and equal roots..");
r1=(-b)/(2.0*a);
r2=(-b)/(2.0*a);
img1=img2=0.0;
}
else
{
printf("Imaginary roots..");
r1=(-b)/(2.0*a);
r2=(-b)/(2.0*a);
img1=sqrt(-disc)/(2.0*a);
img2=-img1;
}
}
printf("First root is\n");
printf("Real =%6.2f and Img part= %6.2f\n",r1,img1);
printf("Second root is\n");
printf("Real =%6.2f and Img part= %6.2f\n",r2,img2);
getch();
return 0;
}
}

No comments:

Post a Comment

Convey your thoughts to authors.