Question

CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation ax +bx+c=0. The roots are given by the formula,
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
#include<bits/stdc++.h> 
using namespace std; 


int main() 
{ 
        int a,b,c;
        cout<<"Enter the value of a : ";
        cin>>a;
        cout<<"Enter the value of b : ";
        cin>>b;
        cout<<"Enter the value of c : ";
        cin>>c;
        if (a == 0) 
        { 
                cout << "Invalid"; 
        } 

        int discriminant = b*b - 4*a*c; 
        double s = sqrt(abs(discriminant)); 

        
        if (discriminant == 0) 
        { 
                cout << "Roots are real and same \n"; 
                cout <<"Root = "<<-(double)b / (2*a); 
        }
        
        else if(discriminant < 0) 
        { 
                cout << "Roots are complex \n"; 
                cout << "Root 1 = "<< -(double)b / (2*a) << " + i" << s << "\n" <<"Root 2 = "<<-(double)b / (2*a) << " - i"<< s; 
        }  
        else
        { 
                cout << "Roots are real and distinct \n"; 
                cout <<"Root 1 = " <<(double)(-b + s)/(2*a) << "\n"<<"Root 2 = "<<(double)(-b - s)/(2*a); 
        } 
        return 0; 
} 

The above is the code that asks the user to enter the a,b,c values in a quadratice equation and then it will check if a=0 then if a =0 it prints invalid else it will caculate the discriminant and check if it is =0 or <0 or >0.If it is equal to 0 then it prints " Roots are equal" and the root and if <0 it prints "Roots are complex" and the roots and if >0 then it prints rools are real and distinct and print the roots.

HERE INTENDATION IS NOT NEEDE IN C++ SO IAM NOT PROVIDING THE SCREENSHOT OF THE CODE AND ALSO I CANNOT GIVE IT IN A TEXT FILE AS THERE IS NO OPTION TO GIVE TEXT FILE.

SCREENSHOT OF THE OUTPUT:

FOR EQUAL ROOTS (D=0):

input Enter the value of a 1 Enter the value of b: - 4 Enter the value of c: 4 Roots are real and same Root = 2 codd o ... Pr

FOR REAL AND DISTINCT(D>0)

input Enter the value of a : 1 Enter the value of b: -5 Enter the value of c: 6 Roots are real and distinct Root 1 = 3 Root 2

FOR IMAGINARY ROOTS OR COMPLEX ROOTS(D<0):

Enter the value of a : 1 Enter the value of b: 4 Enter the value of c: 5 Roots are complex Root 1 = -2 + i2 Root 2 = -2 i2 I

Add a comment
Know the answer?
Add Answer to:
CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. 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...

  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

  • The two roots of the quadratic equation ax2 + bx + c = 0 can be...

    The two roots of the quadratic equation ax2 + bx + c = 0 can be found using the quadratic formula as -b+ v62 – 4ac . -b-v6² – 4ac 1 X1 = - and x2 = 2a 2a When b2 – 4ac < 0 this yields two complex roots - -b V4ac – 62 -b Vac – 6² x1 = = +. . 2a 2a i. and x2 = . za 2al Using the quadratic formula the roots of...

  • please answer this question with python. Write a program to solve the quadratic equation ax^2 +...

    please answer this question with python. Write a program to solve the quadratic equation ax^2 + bx + c = 0 using the standard quadratic formula x = -b plusminus Squareroot b^2 - 4ac/2a or the alternative formula x = 2c/-b Squareroot b^2 - 4ac. Your program should accept values for the coefficients a, b, and c as input and produce the two roots of the equation as output. Your program should detect when the roots are imaginary, but need...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • reword m the program into a design document diregard the m Write a C++ program that...

    reword m the program into a design document diregard the m 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 + b2 - 4ac 2a 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...

  • Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does...

    Java Programming Question 4 (10 points): Solutions for a quadratic equation ax(squared)+bx+c= 0. where a does not equal zero are as follows. r1=( −b+√b(squared)−4ac)/2a r2=(−b−√b(squared)−4ac)/2a if b(squared)−4ac <0, equation doesn’t have real roots. If it is 0 there is one root(r1=r2). Write a Java program to read a,b and c from keyboard and find the roots, if they exist. Note: You need to have a method that takes 3 real values as arguments

  • 4-6 on matlab 4. Write a program in a script file that determines the real roots...

    4-6 on matlab 4. Write a program in a script file that determines the real roots of a quadratic equation ax2+bx+c 0 When the file runs, it asks the user to enter the values of the constants a, b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by: D b2-4ac When D 0, the program displays message "The equation has two roots," and the roots are displayed in the next line. When...

  • Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c...

    Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and c that represent three coefficients. . An initializer for the arguments for a, b, and c. • Three getter methods for a, b, and c. • A method named get Discriminant() that returns the discriminant, which is b- 4ac The methods named getRoot 1() and getRoot 2() for returning the two roots of...

  • A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

    A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT