C Exercises: Calculate the root of a Quadratic Equation
11. Quadratic Equation Roots
Write a C program to calculate the root of a quadratic equation.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
#include <math.h> // Include the math library for mathematical functions.
void main()
{
int a, b, c, d; // Declare variables to store coefficients and discriminant.
float x1, x2; // Declare variables to store roots.
printf("Input the value of a, b & c : "); // Prompt user for input.
scanf("%d%d%d", &a, &b, &c); // Read and store coefficients in 'a', 'b', and 'c'.
d = b*b - 4*a*c; // Calculate the discriminant.
if(d == 0) // Check if discriminant is zero.
{
printf("Both roots are equal.\n");
x1 = -b / (2.0 * a); // Calculate the single root.
x2 = x1;
printf("First Root Root1 = %f\n", x1); // Print the roots.
printf("Second Root Root2 = %f\n", x2);
}
else if(d > 0) // Check if discriminant is positive.
{
printf("Both roots are real and different.\n");
x1 = (-b + sqrt(d)) / (2 * a); // Calculate the first root.
x2 = (-b - sqrt(d)) / (2 * a); // Calculate the second root.
printf("First Root Root1 = %f\n", x1); // Print the roots.
printf("Second Root Root2 = %f\n", x2);
}
else // If discriminant is negative.
printf("Roots are imaginary;\nNo Solution. \n"); // Print no solution message.
}
Output:
Input the value of a,b & c : 1 5 7 Root are imaginary; No Solution.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to calculate the roots of a quadratic equation and handle real and complex roots.
- Write a C program to solve a quadratic equation without using the sqrt() function by implementing a custom square root routine.
- Write a C program to compute quadratic roots and classify them as distinct, equal, or imaginary.
- Write a C program to calculate the roots of a quadratic equation and ensure that the coefficient of the squared term is non-zero.
Go to:
PREV : Admission Eligibility Check.
NEXT : Student Marks and Division Calculation.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.