Mathematics : Roots Of A Quadratic Equation With Degree 2

/*====Quadratic Equations With Degree 2====*/
/*====Only Real Roots (Not Imaginary)====*/

#include<math.h>
#include<stdio.h>
#include<conio.h>
int main()
{
float a,b,c,x1,x2,x3;
int flag=0;
clrscr();
printf("\n====Quadratic Equations (without imaginary roots)====\n\n");
printf("Enter the value of a=\n");
scanf("%f",&a);
printf("Enter the value of b=\n");
scanf("%f",&b);
printf("Enter the value of c=\n");
scanf("%f",&c);
printf("The quadratic equation is : (%f)*x*x + (%f)*x + (%f)\n",a,b,c);
if(a==0 && b==0)
{
printf("Not a valid equation!\n");
flag=1;
}
if(a==0 && b!=0)
{
x1=-(c/b);
printf("The only root is : %f",x1);
flag=1;
}
if(flag==0)
{
if((b*b-4*a*c)>=0)
{
x2=(b*b)-(4*a*c);
x1=(-b+sqrt(x2))/(2*a);
x3=(-b-sqrt(x2))/(2*a);
printf("root1 : %f",x1);
printf("\nroot2 : %f",x3);
}
if((b*b-4*a*c)<0)
{
printf("Not a real root,\ni.e. the root contains imaginary part.");
}
}
getch();
return 0;
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?


 

Leave a comment