C program to find roots of quadratic equation

Mr Coder September 16, 2012 6

C program to find roots of quadratic equation : In mathematics, quadratic equation is second degree univariate polynomial equation. A general quadratic equation can be represented by :

Quadratic Equation

where a, b and c are the coefficients of quadratic equation and a ≠ 0 . If a = 0 then it will be linear equations.

Now the solutions of quadratic equation can have distinct roots, equal roots or complex roots depending upon the value of discriminant.

Discriminant of a quadratic equation is an constant value equal to b^2 -4ac. Now,

If Discriminant>0, then quadratic equation will have distinct roots.

If Discriminant=0, then Quadratic equation will have equal roots.

If Discriminant<0, then Quadratic equation will have complex roots.

I have written a C program to find roots of quadratic equation which accepts coefficients of quadratic equation as input and  calculates and displays the roots of quadratic equation.

Platform : C Language

Compiler : Dev C++

C program to find roots of quadratic equation :

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>

int main()
{
float a,b,c,x1,x2,disc;
printf("-------------------------------------------------------------\n");
printf("----------------made by C code champ ------------------------\n");
printf("-------------------------------------------------------------\n");
printf("\n  C Program for Calcualting roots of Quadratic Equation\n\n"); 
printf("Enter coefficient of X^2 : ");
scanf("%f",&a);
printf("\nEnter coefficient of X^1 : ");
scanf("%f",&b);
printf("\nEnter coefficient of X^0 : ");
scanf("%f",&c);
/*Finding discriminant*/
disc=b*b-4*a*c;
/*distinct roots*/
if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\nThe roots are distinct\n");
printf("x1=%f\nx2=%f\n",x1,x2);
getch();
exit(0);
}
/*Equal roots*/
if(disc==0)
{
x1=x2=-b/(2*a);
printf("\nThe roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
getch();
exit(0);
}
/*complex roots*/
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("\nThe roots are complex.\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
getch();
}

We hope you all have enjoyed the C program to find roots of quadratic equation. If you have any issues or concerns with the logic ask us in form of comments.

6 Comments »

  1. Wiseman October 19, 2012 at 1:05 am - Reply

    A wonrdeful job. Super helpful information.

  2. Android os November 3, 2012 at 9:00 pm - Reply

    I every time emailed this website post page to all my contacts, as if like to read it afterward my links will too.

  3. Ariane Poinsett November 6, 2012 at 2:55 pm - Reply

    Nice post. I learn something new and challenging on websites I stumbleupon everyday. It will always be exciting to read articles from other writers and use something from their websites.

  4. Stand apart November 6, 2012 at 5:40 pm - Reply

    just beneath, are numerous totally not related sites to ours, however, they are surely worth going over.

  5. Sobhan January 31, 2013 at 10:52 am - Reply

    plz explain the code
    x2=sqrt(fabs(disc))/(2*a);
    what is “fabs” meaning?

  6. Lokesh Singh January 31, 2013 at 1:06 pm - Reply

    Fabs function is function absolute value function defined under math.h. It is used to calculate absolute value of arg.
    Say num = -1234.00 then its absolute value will be 1234 i.e. fabs(num) =1234.

Leave A Response »