Write a c code to find the roots of a quadratic equation given coefficients a,b,c and…
- User
Write a c code to find the roots of a quadratic equation given coefficients a,b,c and print if complex numbers as i0.567 in main function onlu
Assistant
Here is the C code to find the roots of a quadratic equation given coefficients a, b, c and print if complex numbers with i:
c#include <stdio.h> #include <math.h> int main() { double a, b, c, d, root1, root2, im; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); d = b*b - 4*a*c; if(d > 0) { root1 = (-b + sqrt(d)) / (2*a); root2 = (-b - sqrt(d)) / (2*a); printf("Two real and distinct roots are: %.2lf, %.2lf", root1, root2); } else if(d == 0) { root1 = -b / (2*a); printf("Two real and equal roots are: %.2lf", root1); } else { im = sqrt(-d)/(2*a); root1 = -b/(2*a); root2 = root1; printf("Two complex roots are: %.2lf+i%.2lf", root1, im); } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.