C program to find the roots of a quadratic equation
This program is written is C, not C++. File should have a .c extension. Make broad use of comments so that I can understand the program. This program should be written with the use of "if" statements, not loops. I have a similar assignment to turn in, so the instructions must be thoroughly followed.
Details about what to do:
Use scanf to read values for double variables a, b, and c, which are assumed to be the coefficients of a quadratic equation.
Print the input numbers in the form of a quadratic equation, using the form shown in the example section below.
If a is equal to zero, print a message that this is linear equation. In case b is not zero, there is a single root: -c/b, and if b does equal zero, there is no root. Print the value of this root or print that there is no root. In this case, your program should not do any of the following items.
Otherwise, assuming a is not zero, calculate the discriminant d given by d = b2 - 4*a*c
If the discriminant is greater than zero, then there are two real roots. Print a message that says this, use the quadratic formula to calculate their values, r1 and r2, and print these two values.
In case the discriminant is equal to zero, there is a single repeated real root. Print a message that says this, use the quadratic formula to calculate the value, and print this value.
If the discriminant is less than zero, then there are two imaginary roots. Print a message that there are two imaginary roots and then print the real and imaginary part of each root. The real part of both roots is the same: -b/(2*a), while the imaginary parts are sqrt(-d)/(2*a) and -sqrt(-d)/(2*a). Notice that we have d < 0, so -d > 0, and it makes sense to take the square root of -d. (It is important for you to realize that the C language has no built-in facilities to handle complex numbers. We are simply working with real numbers, and calculating real and imaginary parts (both reals) of the roots as complex numbers.)
--------------------------------------------------------------------------------
Sample input and output: You may use a separate run for each input triple of numbers, since we haven't studied loops yet. Below in bold are the three numbers that you enter for each separate run. Everything else is what your program should output. You should try each of these 8 sets of inputs and your answers should be the same. For full credit, your output should look exactly the same as the output shown.
Input 3 numbers for a, b, and c: 0.0 2.0 4.0
This is a linear equation (no quadratic term)
Root: -2.000000
Input 3 numbers for a, b, and c: 0.0 0.0 -2.0
This is a linear equation (no quadratic term)
There is no root
Input 3 numbers for a, b, and c: 1.0 -4.0 4.0
There is a single repeated root
Root: 2.000000
Input 3 numbers for a, b, and c: 1.0 -4.0 -12.0
There are two real roots
Root 1: 6.000000
Root 2: -2.000000
Input 3 numbers for a, b, and c: 1.0 -6.0 25.0
Roots are complex conjugates
Root 1: Real part: 3.000000, imaginary part: 4.000000
Root 2: Real part: 3.000000, imaginary part: -4.000000
Input 3 numbers for a, b, and c: 0.5 0.0 -3.0
There are two real roots
Root 1: 2.449490
Root 2: -2.449490
Input 3 numbers for a, b, and c: 3.5 -4.0 -2.75
There are two real roots
Root 1: 1.626059
Root 2: -0.483202
Input 3 numbers for a, b, and c: 1.0 -3.0 3.0
Roots are complex conjugates
Root 1: Real part: 1.500000, imaginary part: 0.866025
Root 2: Real part: 1.500000, imaginary part: -0.866025
Solution Preview
Here below is the code. I also have it in an attched root.c.doc, in case there is some error in the upload.
To compile this program, type "cc root.c -lm"
To execute it , type "a.out"
#include <stdio.h>
#include <math.h>
int main(int argc, char** argv)
{
float a, b, c,d, root1, root2, real_part;
printf("Input 3 numbers for a,b, and c:");
scanf("%f %f %f", ...