C Program to find the Roots of a Quadratic Equation

The Standard form of a Quadratic Equation is

ax2 + bx + c = 0

Here is the example program to find the roots of a quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float real,imag,r1,r2;
printf("\n\t\tQUADRATIC EQUATION");
printf("\n\n Enter the values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>0)
{
printf("\n real and disc roots");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\n root1=%.2f",r1);
printf("\n root2=%.2f",r2);
}
else if(d==0)
{
printf("\n Roots are Equal");
r1=-b/(2*a);
r2=r1;
printf("\n root1= %.2f",r1);
printf("\n root2= %.2f", r2);
}
else if(d<0)
{
printf("\n Roots are Complex ");
real=-b/(2*a);
imag=sqrt(-d)/(2*a);
printf("\nroot1= %.2f+%.2fi  \t root2= %.2f-%.2fi",real,imag,real,imag);
}
getch();
}

Output:

QUADRATIC EQUATION

Enter the values of a,b,c:5
6
1

 real and disc roots
 root1=-0.20
 root2=-1.00

How to solve Quadratic Equation in real time

Quadratic equation is: 5×2 + 6x + 1 = 0

Solution:

a = 5, b = 6, c = 1

x = −b ± √(b2 − 4ac)2a

x = −6 ± √(62 − 4×5×1)2×5

x = −6 ± √(36 − 20)10

x = −6 ± √(16)10

x = −6 ± 410

Ans: x = −0.2 or −1

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *