Learning how to solve quadratic equations programmatically adds a new layer to your coding skills. In this post, we’ll write a C program for quadratic equation solutions that computes the roots of any equation in the form ax² + bx + c = 0.
Using the discriminant, the program identifies whether the roots are real, equal, or complex, giving you a practical approach to handling mathematical equations in C. With this program to find roots of a quadratic equation in C, you’ll not only sharpen your math skills but also gain insight into building logic for engineering and scientific computations.
Math and code make a powerful combination, With roots, your coding passion will see elevation!
Option 1: C Program to Find Roots of Quadratic Equation
#include <stdio.h>
// Used for mathematical functions like sqrt()
#include <math.h>
int main() {
// Declare variables to store coefficients and results
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
// Take input from the user
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate the discriminant (b² - 4ac)
discriminant = b * b - 4 * a * c;
// Discriminant is greater than 0 → real and distinct roots
if (discriminant > 0) {
// First root
root1 = (-b + sqrt(discriminant)) / (2 * a);
// Second root
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2f and %.2f", root1, root2);
// iscriminant is equal to 0 → real and equal roots
} else if (discriminant == 0) {
// Both roots are same
root1 = root2 = -b / (2 * a);
printf("Roots are real and equal: %.2f and %.2f", root1, root2);
// Discriminant is less than 0 → complex roots
} else {
// Real part of root
realPart = -b / (2 * a);
// Imaginary part of root
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi",
realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}Output:
Enter coefficients a, b, and c: 1 4 8
Roots are complex: -2.00 + 2.00i and -2.00 - 2.00i
Explanation:
This C program to find roots of a quadratic equation helps you calculate and classify the roots efficiently using conditional logic and mathematical functions.
Include Libraries: #include <stdio.h> and #include <math.h> for standard input/output and mathematical functions.
Variable Declaration: Declare a, b, c for coefficients and other variables for discriminant, roots, and complex parts.
Calculate Discriminant: discriminant = b * b - 4 * a * c.
Conditional Check: If discriminant > 0: Roots are real and distinct, calculated using (-b ± sqrt(discriminant)) / (2 * a).
If discriminant == 0: Roots are real and equal, calculated as -b / (2 * a).
Else (complex roots): Roots are complex, with realPart = -b / (2 * a) and imaginaryPart = sqrt(-discriminant) / (2 * a).
Print Results: Display roots based on the discriminant value.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 2: C Program for Quadratic Equation Using Function
#include <stdio.h>
#include <math.h>
// Function to calculate and display roots
// of the quadratic equation
void findRoots(float a, float b, float c) {
// Calculate discriminant (b² - 4ac)
float discriminant = b * b - 4 * a * c;
// Discriminant > 0 → real and distinct roots
if (discriminant > 0) {
printf("Real and Distinct Roots: %.2f and %.2f",
// First root
(-b + sqrt(discriminant)) / (2 * a),
// Second root
(-b - sqrt(discriminant)) / (2 * a));
// Discriminant == 0 → real and equal roots
} else if (discriminant == 0) {
printf("Real and Equal Roots: %.2f",
// Both roots are same
-b / (2 * a));
// Discriminant < 0 → complex roots
} else {
printf("Complex Roots: %.2f ± %.2fi",
-b / (2 * a), // Real part
sqrt(-discriminant) / (2 * a)); // Imaginary part
}
}
int main() {
float a, b, c;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
findRoots(a, b, c);
return 0;
}Output:
Enter coefficients a, b, and c: 1 -3 2
Real and Distinct Roots: 2.00 and 1.00
Explanation:
This C program for roots of a quadratic equation uses a separate function, findRoots, to calculate and print the roots, making it modular and reusable.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 3: Quadratic Equation Program in C Using Ternary Operator
#include <stdio.h>
#include <math.h>
int main() {
// Variables to store coefficients and discriminant
float a, b, c, discriminant;
// Take input from user
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate discriminant (b² - 4ac)
discriminant = b * b - 4 * a * c;
// Use ternary operator to check different
// cases of roots
(discriminant > 0) ?
printf("Real Roots: %.2f and %.2f",
(-b + sqrt(discriminant)) / (2 * a), // First root
(-b - sqrt(discriminant)) / (2 * a)) // Second root
: (discriminant == 0) ?
printf("Equal Roots: %.2f",
-b / (2 * a)) // Both roots are same
: printf("Complex Roots: %.2f ± %.2fi",
-b / (2 * a), // Real part
sqrt(-discriminant) / (2 * a)); // Imaginary part
return 0;
}Output:
Enter coefficients a, b, and c: 1 2 5
Complex Roots: -1.00 ± 2.00i
Explanation:
This roots of quadratic equation in C program uses the ternary operator to concisely handle conditions based on the discriminant. It reduces code size but retains readability.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 4: Find Roots of Quadratic Equation in C Using Macros
#include <stdio.h>
#include <math.h>
// Macro to calculate discriminant (b² - 4ac)
#define DISCRIMINANT(b, a, c) ((b) * (b) - 4 * (a) * (c))
int main() {
float a, b, c, disc;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate discriminant using macro
disc = DISCRIMINANT(b, a, c);
// Discriminant > 0 → real and distinct roots
if (disc > 0) {
printf("Real Roots: %.2f and %.2f",
(-b + sqrt(disc)) / (2 * a), // First root
(-b - sqrt(disc)) / (2 * a)); // Second root
// Discriminant == 0 → real and equal roots
} else if (disc == 0) {
printf("Equal Roots: %.2f",
-b / (2 * a)); // Both roots are same
// Discriminant < 0 → complex roots
} else {
printf("Complex Roots: %.2f ± %.2fi",
-b / (2 * a), // Real part
sqrt(-disc) / (2 * a)); // Imaginary part
}
return 0;
}Output:
Enter coefficients a, b, and c: 1 -2 1
Equal Roots: 1.00
Explanation:
This quadratic equation in C language program uses a macro to calculate the discriminant, simplifying root calculations and making the code more readable.
Time Complexity Analysis
Time Complexity : O(1)
Space Complexity Analysis
Space Complexity : O(1)
Option 5: Find Roots of Quadratic Equation in C Using Formula
This method avoids precision loss when b² is very large
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, d, q, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate discriminant
d = b * b - 4 * a * c;
// Check if roots are real
if (d >= 0) {
// Numerically stable computation
// Avoids precision loss when b is large
q = -0.5 * (b + (b > 0 ? sqrt(d) : -sqrt(d)));
// Calculate roots using stable formula
root1 = q / a;
root2 = c / q;
// Print roots
printf("Roots: %.2lf and %.2lf", root1, root2);
} else {
// Complex case not handled here
printf("Complex roots not handled in this method");
}
return 0;
}Output:
Enter coefficients a, b, and c: 1 -2 1
Equal Roots: 1.00Explanation:
This method improves accuracy by avoiding subtraction of large numbers.
It uses a helper variable q to calculate roots more safely.
Best suited for large values and scientific calculations.
Option 6: Find Roots of Quadratic Equation in C Using Newton-Raphson Method
#include <stdio.h>
#include <math.h>
// Function representing equation f(x) = ax² + bx + c
double f(double x, double a, double b, double c) {
return a * x * x + b * x + c;
}
// Derivative of function f'(x)
double df(double x, double a, double b) {
return 2 * a * x + b;
}
int main() {
double a, b, c, x, x1;
int i;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
// Initial guess for root
x = 0;
// Perform iterations to improve accuracy
for (i = 0; i < 20; i++) {
// Newton-Raphson formula
x1 = x - f(x, a, b, c) / df(x, a, b);
// Update value
x = x1;
}
printf("One root (approx): %.4lf\n", x);
return 0;
}Output:
Enter coefficients a, b, and c: 1 2 5
One root (approx): -0.0630Explanation:
This method finds roots using iteration instead of formula.
It starts with a guess and improves it step-by-step.
Commonly used in numerical methods and engineering problems.
Time Complexity Analysis
Time Complexity : O(k)
Space Complexity Analysis
Space Complexity : O(1)
Option 7: Find Roots of Quadratic Equation in C Using Recursion
#include <stdio.h>
// Recursive function to calculate square
// root using Newton's method
double sqrtRec(double n, double guess) {
// Base case: if guess is accurate enough, return it
if ((guess * guess - n < 0.00001) && (guess * guess - n > -0.00001))
return guess;
// Recursive call to improve guess
return sqrtRec(n, (guess + n / guess) / 2);
}
int main() {
double a, b, c, d, root1, root2;
// Take input from user
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate discriminant
d = b * b - 4 * a * c;
// Check for real roots
if (d >= 0) {
// Use recursive function to find square root
double sqrtD = sqrtRec(d, d / 2);
// Calculate roots using formula
root1 = (-b + sqrtD) / (2 * a);
root2 = (-b - sqrtD) / (2 * a);
// Print roots
printf("Roots: %.2lf and %.2lf", root1, root2);
} else {
// Complex roots not handled
printf("Complex roots not handled here");
}
return 0;
}Output:
Enter coefficients a, b, and c: 1 -3 2
Roots: 2.00 and 1.00Explanation:
This method uses recursion to compute square root instead of sqrt().
It repeatedly improves the guess until it becomes accurate.
Useful for learning recursion + mathematical approximation.
Time Complexity Analysis
Time Complexity : O(log n)
Space Complexity Analysis
Space Complexity : O(log n)
No comments:
Post a Comment