 C Program to Find Roots of a Quadratic Equation
• Post author:
• Post category:C Programs

In algebra quadratic equation refers to any equation that can be arranged in the form of ax2+bx+c=0. The formula to solve the quadratic equation is as follows:

-b+sqrt(b2-4ac)/2a or -b-sqrt(b2-4ac)/2a

You can also calculate the roots of a quadratic equation using c programming language. Below is the program to find roots of quadratic equation.

Roots of a Quadratic Equation in C Programming

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d;
double root_1,root_2;
printf("\n\nEnter the Values of A, B, C, Where (a*X*X) + ( b*X ) + (c) = 0\n");
scanf("%d%d%d",&a,&b,&c);
d= (b*b)-(4*a*c);
if( d>=0 )
{
root_1=(-b+sqrt(d))/(2*a);
root_2=(-b-sqrt(d))/(2*a);
printf("First root = %.2lf\n", root_1);
printf("Second root = %.2lf\n", root_2);
}
else
{
printf("First Root = %.2lf + i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second Root = %.2lf - i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
return 0;
}
Enter the Values of A, B, C, Where (a*X*X) + ( b*X ) + (c) = 0
1 -2 1
First root = 1.00
Second root = 1.00