find the roots of the equation ax^2 + bx + c using C language
#include<stdio.h>
#include<math.h>
/* find the roots of the equation ax^2 + bx + c */
int main(){
float a,b,c,discriminant,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
discriminant = b * b - 4 * a * c;
if(discriminant < 0)
{
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.f%+.fi",-b/(2*a),sqrt(-discriminant)/(2*a));
printf(", %.f%+.fi",-b/(2*a),-sqrt(-discriminant)/(2*a));
return 0;
}
else if(discriminant==0 )
{
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.f ",root1);
return 0;
}
else if(discriminant > 0)
{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(discriminant)) / (2* a);
root2 = ( -b - sqrt(discriminant)) / (2* a);
printf("Roots of quadratic equation are: %.f , %.f",root1,root2);
}
return 0;
}
// by Animesh//
Comments
Post a Comment