C program to Find the Roots of a Quadratic Equation
How to write a C program to find the roots of a Quadratic Equation?
Writing a C program to Find the Roots of a Quadratic Equation is pretty easy if you know the basic knowledge about what is it and how its works. A Quadratic equation is a second degree equation in x in Mathematics.General representation of a Quadratic Equation:
ax2 + bx + c = 0, where a, b and c are real constant numbers and a != 0
Syntax:
discriminant = b * b - 4 * a * c; root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a);
Working of Program :
In the program, we will require some numbers from the user or pre-defined variables depending on the program requirement to display the roots of a Quadratic Equation.
Steps to write C program to Find the Roots of a Quadratic Equation :
- Step 1: Start
- Step 2: Declare the variable num1=2, num2=4 and num3.
- Step 3: Read input num1, num2 and num3 from the user or predefine it according to the need.
- Step 4: Find the discriminant using the formula provided.
- Step5: Using the discriminant find the roots of the Quadratic Equation.
- Step 6: Print the result of the program compiled.
- Step 7: Program end.
Problem 1:
Write a program to calculate the the roots of a Quadratic Equation in which the variables are pe-defined.
Code:
Run
#include<math.h> #include<stdio.h> int main() { double a=1, b=-45, c=324, discriminant, root1, root2, real, imag; 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("root1 = %.2lf and root2 = %.2lf", root1, root2); } // condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } // if roots are not real else { real = -b / (2 * a); imag = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", real, imag, real, imag); } return 0; }
Output
root1 = 36.00 and root2 = 9.00
Problem 2:
In the following program we will display the roots of a Quadratic Equation using user defined variables.
Code:
Run
#include<math.h> #include<stdio.h> int main() { double a, b, c, discriminant, root1, root2, real, imag; 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("root1 = %.2lf and root2 = %.2lf", root1, root2); } // condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } // if roots are not real else { real = -b / (2 * a); imag = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", real, imag, real, imag); } return 0; }
Input:
Enter coefficients a, b and c: 1 -45 365
Output:
root1 = 34.38 and root2 = 10.62
Problem 3:
Write a program to calculate the roots of a Quadratic Equations using functions.
Code:
Run
#include <math.h> #include <stdio.h> #include <stdlib.h> void Roots(int a, int b, int c) { if (a == 0) { printf("Invalid"); return; } int d = b * b - 4 * a * c; double sqrt_val = sqrt(abs(d)); if (d > 0) { printf("Roots are real and different "); printf("%f%f", (double)(-b + sqrt_val) / (2 * a), (double)(-b - sqrt_val) / (2 * a)); } else if (d == 0) { printf("Roots are real and same "); printf("%f", -(double)b / (2 * a)); } else // d < 0 { printf("Roots are complex "); printf("%f + i%f%f - i%f", -(double)b / (2 * a), sqrt_val/(2 * a), -(double)b / (2 * a), sqrt_val/(2 * a)); } } int main() { int num1 = 1, num2 = -7, num3 = 12; Roots(num1, num2, num3); return 0; }
Output
Roots are real and different 4.0000003.000000
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment