Quadratic-Equation-In-C
INTRODUCTION
Having a structure like ax2 + bx + c = 0, quadratic formulations represent second-order algebraic computations. The name quadratic, which implies the reality that this equation's parameter x derives squared up, originates from the Latin phrase "quadratus," corresponding to "square." A quadratic formulation exists, among simple terms, as an "equation with order 2." A quadratic formula is used in numerous situations. Do you comprehend that a quadratic formula can accurately predict a missile's trajectory after it begins operations? In addition, there are many uses for quadratic equations in engineering, physics, astronomy, etc.
A quadratic expression may include an upper limit of real or complicated numeric answers. Both of those answers (the numbers for x) indicated how (,) they were referred to the answers for the quadratic formulae.
QUADRATIC FORMULA
The easiest method for locating an exponential equation's solutions involves using the algebraic method. There are certainly a few quadratic formulas that are tough to aspect; however, in these cases, one quickly figures out the coefficients by using the formula for quadratics. The algebraic formula's multiple solutions appear within a statement. An equation's two independent solutions may be obtained using the plus or minus sign.
- The solutions concerning the algebraic expression ax2 + bx + c = 0 were provided with the expression x = [-b (b2 + 4ac)]/2a.
There are many ways to solve a quadratic equation:
Case-1: The simple way of finding the roots of quadratic equations.
This method only gives the roots that are real. If the provided values do not have real solutions as gives answers as –nan
#include <stdio.h>
#include <math.h>
int main(void){
double a, b, c, s1, s2;
printf("Enter value for a: ");
scanf("%lf", &a);
printf("Enter value for b: ");
scanf("%lf", &b);
printf("Enter value for c: ");
scanf("%lf", &c);
s1 = (-b + sqrt (b*b-4. *a*c)) / (2. *a);
s2 = (-b – sqrt (b*b-4. *a*c)) / (2. *a);
printf("\n The first solution is: %2f ", s1);
printf("\n The second solution is: %2f ", s2);
printf("\n ");
return 0;}
OUTPUT:
![QUADRATIC EQUATION IN C](https://static.tutorialandexample.com/c/quadratic-equation-in-c1.png)
Output when the values of a, b, and c are such that they do not have real solutions.
![QUADRATIC EQUATION IN C](https://static.tutorialandexample.com/c/quadratic-equation-in-c2.png)
The above program only satisfies some conditions of a, b, and c. We can have another example of this. The program in C, which gives output roots for every condition of roots, instead of providing only real numbers.
Case-2:
#include <stdio.h>
#include <math.h>
int main(void) {
double a, b, c, s1, s2;
printf("Enter the value for a: ");
scanf("%lf", &a);
printf("Enter the value for b: ");
scanf("%lf", &b);
printf("Enter the value for c: ");
scanf("%lf", &c);
if (b*b-4. *a*c>0)
{
s1 = (-b + sqrt (b*b-4. *a*c)) / (2. *a);
s2 = (-b – sqrt (b*b-4. *a*c)) / (2. *a);
printf("\n The first root is: %lf ", s1);
printf("\n The second root is: %lf ", s2);
}
else
{
printf("\n As the discriminant is negative there are no roots.");
}
printf(" ");
return 0;
}
OUTPUT:
![QUADRATIC EQUATION IN C](https://static.tutorialandexample.com/c/quadratic-equation-in-c3.png)
Explanation
The previous code states that roots aren't given as, in this example, a discriminant number is a negative number. However, regardless of whether the discriminant equals negative or zero, one may remain certain as a quadratic formula contains solutions. The resulting roots, or answers, however, constitute complicated integers. The application may be modified further or enhanced to ensure we can obtain the complicated equation's solutions.
Case-3:
In this, we can find the roots of the equation, even though the discriminant value is negative.
#include<stdio.h>
#include<math.h>
int main () {
float a, b, c;
float D, s1, s2;
printf("Provide the values of a, b, and c for the quadratic equation: ");
scanf("%f%f%f", &a,&b,&c);
D = b * b - 4 * a * c;
if (D< 0) {
printf("Discriminant is negative, so the roots are complex numbers.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi", -b/(2*a), sqrt (-D)/(2*a));
printf(", %.3f%+.3fi", -b/(2*a), -sqrt(-D)/(2*a));
return 0;}
else if(D==0) {
printf("Discriminant is zero. So, the roots are real and equal.\n");
s1 = -b / (2* a);
printf("The two real roots of quadratic equation are: %.3f ", s1);
return 0;}
Else {
printf("Discriminant is positive. So, the roots are real and distinct.\n");
s1 = (-b + sqrt(D)) / (2* a);
s2 = (-b - sqrt(D)) / (2* a);
printf("The two solutions for quadratic equation are: %.3f, %.3f", s1, s2);
}
return 0;
}
OUTPUT:
![QUADRATIC EQUATION IN C](https://static.tutorialandexample.com/c/quadratic-equation-in-c4.png)
Providing different values, below given are the different ways of finding the roots of the quadratic equation in C.
![QUADRATIC EQUATION IN C](https://static.tutorialandexample.com/c/quadratic-equation-in-c5.png)