Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

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
****************************************************************************************

Enter the value of a,b,c


1 -3 2
a = 1 b = -3 c = 2
D = 1.000000
Roots are real and distinct
R1 = 1.000000 R2 = 2.000000
Enter the value of a,b,c
121
a=1 b=2 c=1
D = 0.000000
Roots are real and same
R1 = -1.000000 R2 = -1.000000
Enter the value of a,b,c
126
a=1 b=2 c=6
D = -20.000000
Roots are imaginary
R1 = -1.000000 + 2.236068i
R2 = -1.000000 - 2.236068i

RESULT:
All the three cases of roots have been discussed above and the respective roots have
been obtained.

You might also like