Jan 11, 2016

C Program to Solve Quadratic Equation


Algorithm:-

1.Start
2.Accept the coefficients a,b,c
3.If (a|==0) goto step 4 else exit
4.If b^2>4*a*c
   Find roots x1 and x2
   Display both roots.
   Else goto step 5
5.If b^2=4*a*c
   Find root x
   Display the root.
   Else goto step 6.
6.Display roots are i,imaginary
7.Stop

Program:-

/*Program to find roots of quadratic equation - Crazy Factzz */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,check;
float x1,x2;
printf("\n Enter the coefficient of x^2 :");
scanf("%f",&a);
if(a==0)
{
printf("\n Not a quadratic equation");
getch();
exit(0);
}
printf("\n Enter the coefficient of  x :");
scanf("%f",&b);
printf("\n Enter the constant :");
scanf("%f",&c);
check=b*b-4*a*c;
if  (check>0)
{
x1=(-b+check)/(2*a);
x2=(-b-check)/(2*a);
printf("\n Equation have two real roots \n Root 1=%5.2f \n Root 2=%5.2f \n",x1,x2);
}
else if (check==0)
{
x1=(-b+check)/(2*a);
printf("\n Equation have two equal roots \n Roots =%5.2f \n ",x1);
}
else if(check<0)
{
printf("\n The roots are Imaginary");
}
getch();
}

For more C programs - click here