Skip to main content

Equation based solve



An equation of the form
ax^2 + bx+c=0
is know as the quadratic equation . The values of x that
satisfy the equation are know as the roots of the equation.
A quadratic equation has two roots which are given by the following two formulae.
root1=(-b+sqrt(b^2-4ac))/2a.
root2=(-b-sqrt(b^2-4ac))/2a
The program requests the user to input the values of a,b and c and output
roots 1 and root2.

  
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    main()
    {
        float a, b, c, discriminant, root1, root2;
    
        printf ("Input values of a, b, and c\n ");
    
        scanf ("%f %f %f",&a, &b, &c);
    
        discriminant=b*b-4*a*c;
    
        if(discriminant<0)
        {    
            printf ("\n \n Roots are Imaginary\n");
        }    
        else
        {
            root1=(-b+sqrt(discriminant) )/(2.0*a);
            root2=(-b-sqrt(discriminant))/(2.0*a);
            printf("\n \n Root1=%5.2f\n\n Root2=%5.2f\n",root1,root2);
        }
        getch();
    }