Write a C Program to Determine the roots of the quadratic equation (ax^2 + bx + c = 0) [Code and Explanation]
Determine the roots of the quadratic equation
Using the well-known quadratic formula
Allow for the possibility that one of the constants has a value of zero, and that the quantity
is less than or equal to zero. Test the program using the following sets of data:
(Code explanation at the bottom of the post)
#include "stdafx.h"
#include <stdio.h>
#include <cmath>
int main()
{
double a = 0, b = 0, c = 0, d, sqD, x1, x2;
printf("Enter a: "); // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");
printf("Enter b: "); // Enter the value for b
scanf_s("%lf", &b);
printf("\n");
printf("Enter c: "); // Enter the value for c
scanf_s("%lf", &c);
printf("\n");
d = pow(b, 2) - (4 * a * c); // we assign (b^2) - 4ac to variable d
if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}
else
{
sqD = sqrt(d); // Square root of d
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}
return 0;
}
Once you run the program in Visual Studio, you will get something like this:
After you input a number for a and press enter, you will be asked to input a number for b, then same process for c:
After you enter the number, if d is imaginary, then an error message will show stating the number is undetermined:
However, if d is real then the program will display the roots just fine:
Ok! let's breakdown the code line by line:
We include the header <cmath> because we are dealing with double\floating numbers , in addition to using the mathematical functions pow and sqrt. We can't use sqrt or pow without the cmath header.
printf("Enter a: "); // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");
printf("Enter b: "); // Enter the value for b
scanf_s("%lf", &b);
printf("\n");
printf("Enter c: "); // Enter the value for c
scanf_s("%lf", &c);
printf("\n");
In this part, we are simply asking the user to enter the values that will be stored in a, b, and c.
pow(b,2) means that we are rising b to the power of 2, b^2
if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}
After d is calculated, we are gonna compare it with 0. If d < 0, then we can't determine the roots because we will have to take square root of d, which will be an imaginary number since d < 0.
If that's the case, then we will have the program prints out a message stating that you can't determine the roots.
else
{
sqD = sqrt(d); // Square root of d
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}
If the calculated value of d is not less than 0, then we proceed to finding the roots using the below equation
as we know [(b^2) - 4ac] is assigned to d, to find the square root of it we use a math function sqrt (stands for Square Root), sqrt(d). We will take that and find the roots and print them out with this code:
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1); // Print out Root 1
printf("Root 2 = %lf\n", x2); // Print out Root 2
and that's it....
Using the well-known quadratic formula
Allow for the possibility that one of the constants has a value of zero, and that the quantity
is less than or equal to zero. Test the program using the following sets of data:
(Code explanation at the bottom of the post)
#include "stdafx.h"
#include <stdio.h>
#include <cmath>
int main()
{
double a = 0, b = 0, c = 0, d, sqD, x1, x2;
printf("Enter a: "); // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");
printf("Enter b: "); // Enter the value for b
scanf_s("%lf", &b);
printf("\n");
printf("Enter c: "); // Enter the value for c
scanf_s("%lf", &c);
printf("\n");
d = pow(b, 2) - (4 * a * c); // we assign (b^2) - 4ac to variable d
if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}
else
{
sqD = sqrt(d); // Square root of d
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}
return 0;
}
Once you run the program in Visual Studio, you will get something like this:
After you input a number for a and press enter, you will be asked to input a number for b, then same process for c:
After you enter the number, if d is imaginary, then an error message will show stating the number is undetermined:
However, if d is real then the program will display the roots just fine:
Ok! let's breakdown the code line by line:
We include the header <cmath> because we are dealing with double\floating numbers , in addition to using the mathematical functions pow and sqrt. We can't use sqrt or pow without the cmath header.
--------------------------------------------------------------------------
printf("Enter a: "); // Enter the value for a
scanf_s("%lf" , &a);
printf("\n");
printf("Enter b: "); // Enter the value for b
scanf_s("%lf", &b);
printf("\n");
printf("Enter c: "); // Enter the value for c
scanf_s("%lf", &c);
printf("\n");
In this part, we are simply asking the user to enter the values that will be stored in a, b, and c.
--------------------------------------------------------------------------
d = pow(b, 2) - (4 * a * c);
Here, we will calculate the equation below and store the value to a variable named dpow(b,2) means that we are rising b to the power of 2, b^2
--------------------------------------------------------------------------
if (d < 0) // we can't have imaginary number, so if d < 0, it gives an error
{
printf("Undetermined, imaginary number");
}
After d is calculated, we are gonna compare it with 0. If d < 0, then we can't determine the roots because we will have to take square root of d, which will be an imaginary number since d < 0.
If that's the case, then we will have the program prints out a message stating that you can't determine the roots.
--------------------------------------------------------------------------
{
sqD = sqrt(d); // Square root of d
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1);
printf("Root 2 = %lf\n", x2);
}
If the calculated value of d is not less than 0, then we proceed to finding the roots using the below equation
as we know [(b^2) - 4ac] is assigned to d, to find the square root of it we use a math function sqrt (stands for Square Root), sqrt(d). We will take that and find the roots and print them out with this code:
x1 = ((-b) + (sqD)) / (2 * a); // Root 1
x2 = ((-b) - (sqD)) / (2 * a); // Root 2
printf("Root 1 = %lf\n", x1); // Print out Root 1
printf("Root 2 = %lf\n", x2); // Print out Root 2
and that's it....