C program to find roots of quadratic equation

C program to find roots of quadratic equation

457
7
SHARE

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

C program to find roots of quadratic equation

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

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.

7 COMMENTS

  1. 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 REPLY