Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a C Program to find the roots of the Quadratic Equation ax2+bx+c

Source Code :

#include<stdio.h>
#include<math.h>
void main()
{
    float a,b,c,dd,d,x,y;
    clrscr();
    printf("Enter the Values of a,b,c of a Quadratic Equation : ");
    scanf("%f %f %f",&a,&b,&c);
    if(a!=0)
    {
        dd=(b*b)-(4*a*c);
        if(dd<0)
        {
            printf("\n\n\t\tThe Roots are Imaginary");
            dd=-dd;
            d=sqrt(dd);
            x=(-b+d)/(2*a);
            y=(-1)*(-b-d)/(2*a);
            printf("\n\nThe two roots of the Quadratic Equation are :
             x=%5.2f(i) and y=%5.2f(i)",x,y);
        }
        else
        {
             d=sqrt(dd);
             x=(-b+d)/(2*a);  //Calculation of x & y
             y=(-b-d)/(2*a);
             printf("\n\nThe two roots of the Quadratic Equation are :
                                    x=%5.2f and y=%5.2f",x,y);
        }
    }
    else
    {
      printf("\n\n\t\t The Value of 'a' Cannot be 0. Retry ");
    }
     getch();
}


Output :


Enter the Values of a,b,c of a Quadratic Equation : 6 2 3

            The Roots are Imaginary

The Two Roots of the Quadratic Equation are : x=0.52(i) and y=0.85(i)

No comments:

Post a Comment