Question

In: Computer Science

Write a C++ program that solves a quadratic equation to find its roots. The roots of...

Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula (–b ± sqrt(b2 – 4ac)) / 2a *Use #include for the use of sqrt. The value of the discriminant (b2 – 4ac) determines the nature of roots. If the value of the discriminant is zero, then the equation has a single real root. If the value of the discriminant is positive then the equation has two real roots. If the value of the discriminant is negative, then the equation has two complex roots. The program takes values of a, b, and c as input and outputs the roots. Be creative in how you output complex roots. Include a loop that allows the user to repeat this calculation for new input values until the user says she or he wants to end the program.

Solutions

Expert Solution


#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void calculate();

int main(){
        
        while(1){
            calculate();
            char opt[2];
            cout << "Do you want to continue - (y/n) :";
            cin >> opt;
            if(opt[0] == 'n')
                break;
        }
        return 1;
}
void calculate(){
        
        int a, b, c;
        cout << "Equation : ax^2 + bx +c = 0" <<endl;
        cout << "a = ";cin >> a;
        cout << "b = ";cin >> b;
        cout << "c = ";cin >> c;
        
        // calculating discriminant
        double discriminant = b*b - 4*a*c;
        
        // checking type of root
        // real root
        // single root
        // imaginary root
        if(discriminant > 0){
            
                double sqrtD = sqrt(discriminant);
                // calculating roots
                double root1 = (-b + sqrtD)/(2*a);
                double root2 = (-b - sqrtD)/(2*a);
                // printing roots
                cout << "Root 1 : " << root1 <<endl;
                cout << "Root 2 : " << root2 <<endl;
                
        }else if(discriminant == 0){
            // calculating root
                double root = -b/(2*a);
                // printing root
                cout <<"Root : " << root <<endl;
        }else if(discriminant < 0){
            discriminant = sqrt(abs(discriminant));
                string root1 = "( "+to_string(-b)+" + "+to_string(discriminant)+"i ) / "+to_string(2*a);
                string root2 = "( "+to_string(-b)+" - "+to_string(discriminant)+"i ) / "+to_string(2*a);
                cout << root1 <<endl;
                cout << "Root 1 : " << root1 <<endl;
                cout << "Root 2 : " << root2 <<endl;
        }       
}

Code

Output


Related Solutions

Write a program to input the coefficients of a quadratic equation and solve roots for all...
Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA. x=(b ^2) - (4ac) I have it for 2 complex and 2 real and repeated. Is that all cases?
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
use Java The two roots of a quadratic equation ax^2 + bx + c = 0...
use Java The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...
Write a Java program that solves a set of quadratic equations as per Requirement #1 (without...
Write a Java program that solves a set of quadratic equations as per Requirement #1 (without using global variables), and reads an integer number entered in the command line, as follows: > java program_name input_number and depending on the value of this number does the following: 1) If the number entered is positive, the program shall solve (running in a loop) as many quadratic equations of the following form, as the input_number tells: a * x2 + b * x...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
Find Roots of a Quadratic Equation Step 1: Analyze the Problem Accept three coefficients a, b,...
Find Roots of a Quadratic Equation Step 1: Analyze the Problem Accept three coefficients a, b, and c (all of data type double) of a quadratic equation: ax2+bx+c=0 Output real roots (double) of the equation Step 2: Develop a Solution Check for degenerate case (user enters 0 for both a and b), no solution Check if the equation is linear (user enters 0 for a), x = -c/b Calculate the discriminant = b2-4ac (obviously, the data type will be double)...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a,...
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a, b and c. Please also display the code you have used. Please use excel UserForm Thanks
Write a full program that solves the following equation and displays the value for x and...
Write a full program that solves the following equation and displays the value for x and y: 3.4x+50.2y=44.5 2.1x+.55y=5.9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT