Professional Documents
Culture Documents
Practical - 03
Practical - 03
AIM:
To write a programme in C to find the roots of quadratic equation.
QUESTION:
Write a programme in C to find roots of quadratic equation ax2 + bx +c.
THEORY:
The roots of a quadratic equation ax2 + bx + c:
Discriminant D=b2−4 ac
If D > 0:
Roots are real and unique and are given by
−b ± √ D
2a
If D = 0:
Roots are real and equal and are given by
−b
2a
If D < 0:
Roots are imaginary and are given by
−b ± i √ D
2a
C – CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float x,D,r1,r2,rp1,rp2,ip1,ip2;
printf("Enter the value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
printf("a = %d\tb = %d\tc = %d\n",a,b,c);
D=b*b-(4*a*c);
printf("D = %f\n",D);
if(D>0)
{
r1 =(-b-sqrt(D))/(2*a);
r2 = (-b+sqrt(D))/(2*a);
printf("Roots are real and distinct\n");
printf("R1 = %f\tR2 = %f\n",r1,r2);
}
else if(D==0)
{
r1 = -b/(2.0*a);
r2 = -b/(2.0*a);
printf("Roots are real and same\n");
printf("R1 = %f\tR2 = %f\n",r1,r2);
}
else
{
rp1 =-b/(2.0*a);
ip1= sqrt(-D)/(2.0*a);
printf("Roots are imaginary\n");
printf("R1 = %f + %fi\n",rp1,ip1);
printf("R2 = %f - %fi\n",rp1,ip1);
}
getch();
}
****************************************************************************************
OUTPUT
****************************************************************************************
RESULT:
All the three cases of roots have been discussed above and the respective roots have
been obtained.