Wednesday, 16 May 2012

Write a C program to find the roots of quadratic equation.

#include<stdio.h>
#include<math.h>
int main()
{    float a,b,c,D,x1,x2,img,z;
    printf("\n Enter the coefficients of qudratic equation:");
    scanf("%f%f%f",&a,&b,&c);
    printf("Quadratic equation is %fx^2+%fx+%f.\n",a,b,c);
    if((b*b-4.0*a*c)>=0)
        D=b*b-4.0*a*c;
    else
        D=4.0*a*c-b*b;
    if((b*b-4.0*a*c)>0)
    {
        x1=(-b+sqrt(b*b-4.0*a*c))/(2.0*a);
        x2=(-b-sqrt(b*b-4.0*a*c))/(2.0*a);
        printf("\n It's roots are real and unequal.They are %f and %f.\n",x1,x2);
    }
   
    if((b*b-4.0*a*c)==0)
    {
        x1=x2=-b/(2.0*a);
        printf("\n It's roots are equal.It is %f.\n",x1);
    }
   
    if((b*b-4.0*a*c)<0)
    {
        z=-b/(2.0*a);
        img=sqrt(D)/(2.0*a);
        printf("\n It's roots are imaginary.They are %f+%fi and %f-%fi.\n",z,img,z,img);
    }
    return 0;
}



Note: 
While taking coefficients a,b,c as input give their value zeros and check the output. What did u get? Make necessary changes in the above program to make it working for all cases. We are leaving it to you as a assignment.

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top