Write a C program to find the roots of a Quadratic equation.
Question
Write a C program to find the roots of a Quadratic equation.
Solution
Sure, here is a simple C program that calculates the roots of a quadratic equation:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
discriminant = b*b-4*a*c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
printf("Roots are %.2lf and %.2lf",root1 , root2);
}
//condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b/(2*a);
printf("Roots are %.2lf and %.2lf", root1, root2);
}
// if roots are not real
else {
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
printf("Roots are %.2lf+%.2lfi and %.2lf-%.2lfi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
This program first calculates the discriminant and then finds the roots based on the value of the discriminant. If the discriminant is greater than 0, the roots are real and different. If the discriminant is equal to 0, the roots are real and equal. If the discriminant is less than 0, the roots are complex and different.
Similar Questions
Input values of a,b,c for a quadratic equation and find roots of the equation..
Wrie a python program to use a function that computes the roots of a quadratic equation
Write an algorithm and draw a flowchart thatwill calculate the roots of a quadratic equation Hint: d = sqrt ( ), and the roots are:x1 = (–b + d)/2a and x2 = (–b – d)/2a2 0ax bx c 2 4b ac
Solving Quadratic Equations
What is the output of the following C Program?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.