Filled Under: ,

C Program to Find the Roots of a Quadratic Equation

C Program to Find the Roots of a Quadratic Equation



C Program to Find the Roots of a Quadratic Equation

We will discuss about the C Program to Find the Roots of a Quadratic Equation ax2+bx+c=0, for all  possible combination of a, b and c. A quadratic equation will have two roots which are obtained using the expressions shown above:



#include<stdio.h>
#include<math.h>
      void main()
{
      float a,b,c,rt1,rt2,r;
      printf("Enter the value of a,b,c ");
      scanf("%f%f%f",&a,&b,&c);
      r=(b*b)-(4*a*c);
     if(r>0)
        {rt1=(-b+sqrt(r))/(2*a);
         rt2=(-b-sqrt(r))/(2*a);
         printf("The roots are real and unequal \n");
         printf("The roots are %f %f \n",rt1,rt2);}
     else if(r==0)
        {rt1=-(b/(2*a));
        printf("The roots are real and equal\n");
        printf("The root is %f ",rt1);}
   else if(r<0)
       {rt1=-(b/(2*a));
       printf("The roots are imaginary\n");}
    else
        {printf("Wrong input");}
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.


0 comments:

Post a Comment

 

Copyright 2020 Programming in C All Right Reserved