Header Ads Widget

Responsive Advertisement

Write a program to determine the roots of quadratic equation.

 #include<stdio.h>

#include<math.h>

int main () 

float a, b, c, d, rooti, root2, img; 

printf ("Enter coefficients a, b and c: "); 

scanf ("%f%f%f",&a,&b,&c); 

d=b*b-(4*a*c); 

if (a == 0 && b == 0 && c == 0) 

printf ("Invalid coefficients\n"); 

printf ("Enter valid inputs\n"); 

else if (d>0) 

rooti=(b + sqrt (d))/ (2*a); 

root2=(-b-sqrt (d))/ (2*a); 

if (a == 0) 

rooti = (-c)/b; 

printf ("Linear equation\n"); printf ("Root = %f\n", rooti); 

else 

printf ("The roots are real and distinct\n"); 

printf ("root1 = %f and root2 = %f\n", rooti, root2); 

else if (d == 0) 

rooti = root2 = b/(2*a); 

printf ("The roots are real and equal\n"); 

printf ("root1 = root2 = %f\n", rooti); 

else if (d<0) 

rooti = root2 = -b/(2*a); 

img = (sqrt (-d))/(2*a); 

printf ("The roots are real and imaginary\n"); 

printf ("root1 = %f + i%f\n", rooti, img); 

printf ("root2 = %f - i%f\n", root2, img); 

return 0; 


Post a Comment

0 Comments