In this tutorial you will learn about the C Program to find Roots of a Quadratic Equation and its application with practical example.
C Program to find Roots of a Quadratic Equation
In this tutorial, we will learn to create a C program that will find the Roots of a Quadratic Equation in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in c programming.
What is a quadratic expression?
A quadratic equation is an algebraic expression of the second degree in x. The quadratic equation in its standard form is ax2 + bx + c = 0,
where a, b are the coefficients, x is the variable, and c is the constant term.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Taking the input quadratic expression from the user. 3. Finding the roots of the numbers using conditional statements 4. Saving the root values. 5. Print the roots of the expressions. 6. End the program. |
Program to find Roots of a Quadratic Equation.
In this program, we will first take the quadratic equation in input from the user for finding the roots. After taking the input, we will find the root of all three numbers using the mathematical expressions. Then we will print the output roots of the program using the “printf() ” function.
Below is an example of quadratic equations.
1 2 3 4 |
For Example :- ax2 + bx + c = 0, where a, b and c are real numbers and a != 0 |
With the help of this program, we can check find Roots of a Quadratic Equation.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* C Program to find Roots of a Quadratic Equation */ //Including the required header files for the program. #include <math.h> #include <stdio.h> int main() { //Declaring the required variable for the program. double a, b, c, dscmt, root1, root2, realPart, imagPart; //a = it will hold double value for the input number. //b = it will hold double value for the input number. //c = it will hold double value for the input number. //Taking the input numbers from the user for the program. printf("Enter coefficients a, b and c: "); //Scanning the input number from the user. scanf("%lf %lf %lf", &a, &b, &c); dscmt = b * b - 4 * a * c; // condition for real and different roots. if (dscmt > 0) { root1 = (-b + sqrt(dscmt)) / (2 * a); root2 = (-b - sqrt(dscmt)) / (2 * a); //Printing for the real and different roots. printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } // condition for real and equal roots. else if (dscmt == 0) { root1 = root2 = -b / (2 * a); //Printing for the real and equal roots. printf("root1 = root2 = %.2lf;", root1); } // if roots are not real. else { realPart = -b / (2 * a); imagPart = sqrt(-dscmt) / (2 * a); //Printing for not real roots. printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- a, b, c = it will hold the input value of the number.
- root1, root2, root3 = it will hold the root values for the numbers.
Taking the input numbers from the user for the quadratic equations.
In this section of the code of the program, we will find the roots for the numbers from the input.
Printing the roots of the given numbers by the user.