Program to find the roots of Quadratic Equation
Source Code:
#include<stdio.h>
int main()
{
int a,b,c,discriminant;
float root1,root2,real,imag;
printf("enter the coefficient of a,b,c\n");
scanf("%d %d %d",&a,&b,&c);
printf("%d x^2+%d x+%d \n",a,b,c);
discriminant=b*b-4*a*c;
if(discriminant>0)
{
root1=(-b+sqrt(discriminant))/(2*a);
root2=(-b-sqrt(discriminant))/(2*a);
printf("root1=%f and root2=%f\n",root1,root2);
}
else if(discriminant==0)
{
root1=root2=-b/(2*a);
printf("root1=root2=%f",root1);
}
else
{
real=-b/(2*a);
imag=sqrt(-discriminant)/(2*a);
printf("root1=%f+%fi and root2=%f-%fi\n",real,imag);
}
return 0;
}
OutPut:
enter the coefficient of a,b,c
1
-2
6
1 x^2+-2 x+6
root1=1.000000+2.236068i and root2=0.000000-0.000000i
No comments:
Post a Comment