/* *Roots of a quadratic equation * Author: rahule@cse.iitk.ac.in * * Created on 13 August, 2010, 10:53 PM */ #include #include int main() { float a,b,c,d,r1,r2; printf("\nEnter the coefficients a,b,c :"); //Enter the coefficinet of the equation ax^2+bx+c scanf("%f%f%f",&a,&b,&c); printf("%f",-a); d=b*b-4*a*c; //finds d=(b^2-4ac) if(d==0) //if b^2-4ac=0 means roots are real and equel { r1=(-b)/(2*a); printf("\nThe roots are real and equel.The root is :%f",r1); //prints the roots } else if(d>0) //if b^2-4ac > 0 means roots are real and distict { r1=((-b)+sqrt(d))/(2*a); //finds the roots r2=((-b)-sqrt(d))/(2*a); printf("\nThe roots are real and distict\nRoot1:%f\nRoot2:%f",r1,r2); //prints the roots } else //b^2-4ac<0 means roots are imaginary { printf("\nThe roots are imaginary\nRoot1:%f+i%f\nRoot2:%f-i%f",(-b)/(2*a),sqrt(-d)/(2*a),(-b)/(2*a),sqrt(-d)/(2*a)); //calculates and prints the imaginary rooots } }